PDA

View Full Version : Windows to Ubuntu migration using Qt



ottawaDan
3rd June 2011, 15:36
Good morning,

I am migrating an application developed for Windows to Ubuntu using Qt-4.6. So far I am making good progress but there are two Windows functions that are giving me trouble.

I have been using QWaitCondition objects to reproduce the behaviour of Windows events (CreateEvent(), SetEvent(), and WaitForSingleObject()). In Windows, for WaitForSingleObject(), if the time-out argument (dwMilliseconds) is zero, the function does not enter a wait state if the object (event) is not signalled; it always returns immediately. The returned value indicates whether the object has been signalled.

I tried to reproduce this behaviour using QWaitCondition::wait() with time-out set to zero without success. The locked mutex is not released and the function always returns false indicating that the wait timed out. I could not find any description related to time-out set to zero either.

What is the corresponding behaviour in Qt for WaitForSingleObject() using time-out set to zero?

My second question is about QThread. Again, the migration is mostly working but I don't know how to reproduce the behaviour of Windows Sleep( 0).
According to the documentation, a value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.

How is this behaviour reproduced using Qt? Do I simply use a sleep statement? In this case, what time value is used? The application is running live image processing and display.

Thanks in advance
Daniel

wysota
3rd June 2011, 16:04
I have been using QWaitCondition objects to reproduce the behaviour of Windows events (CreateEvent(), SetEvent(), and WaitForSingleObject()). In Windows, for WaitForSingleObject(), if the time-out argument (dwMilliseconds) is zero, the function does not enter a wait state if the object (event) is not signalled; it always returns immediately. The returned value indicates whether the object has been signalled.

I tried to reproduce this behaviour using QWaitCondition::wait() with time-out set to zero without success. The locked mutex is not released and the function always returns false indicating that the wait timed out. I could not find any description related to time-out set to zero either.

What is the corresponding behaviour in Qt for WaitForSingleObject() using time-out set to zero?
How about using QSemaphore with QSemaphore::tryAcquire() instead?


How is this behaviour reproduced using Qt?
You can't. Schedulling is managed by the operating system. If you use sleep(0) you thread will go to sleep regardless of whether there are other threads in the application at all.

If I'm guessing correctly what you are trying to do then I'd suggest changing the design to use QtConcurrent::run() or do all the work in one thread with idle processing if required/useful.