[Interest] QThread and sleep

Hugo Drumond Jacob hugo at jacob.eng.br
Tue Mar 19 14:37:56 CET 2013


Hi folks!

Today some doubt has ocurred on my office. Suppose that for some
reason we need to sleep a thread (not the main thread) for some time
(eg. 100 ms). The QThread::wait(int) "provides similar functionality
to the POSIX pthread_join()" and this don't will sleep the target
thread, but the caller thread.

Call the QThread::wait(int) from the target thread is forbidden
(according to line 651 of qthread_unix.cpp).

Furthermore, is "wrong" call QThread::wait(int) except for
synchronously thread termination with QThread::terminate() ? ( I know,
isn't so healthy use QThread::terminate() ) QThread::wait(int) is a
busy wait, right?

The attached code illustrate the question.

My immediate solution is: create some MyThread class inherited of
QThread just for expose the QThread::*sleep(unsigned long) methods and
create some slot or Q_INVOKABLE method on Worker to call these exposed
methods. Anyone propose some more elegant solution?

Thanks!

Hugo Drumond Jacob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ThreadWaitTest.cpp
Type: text/x-c++src
Size: 946 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130319/c9dacc2a/attachment.cpp>


More information about the Interest mailing list