[Interest] Subject: QThread and sleep

Etienne Sandré-Chardonnal etienne.sandre at m4x.org
Tue Mar 19 15:48:53 CET 2013


Hi,

Do you want to force the thread to sleep (ie pause its execution for some
time) from the main thread? I do not think there is a generic solution, you
cannot force a thread to pause.

But:
 - If the thread uses an event loop, you can have a QObject that you create
and move to this thread using moveToThread (from the main thread). Then
create either an event or a slot, which just calls "sleep". Then post the
event, or send a signal connected to the slot, from the main thread.
 - If the thread does not use an event loop, you need to poll for a flag
periodically, which when set, calls sleep().

Apart from this, you cannot force it to pause if he hasn't a management for
a pause event in the code it is running. You could just set it to ultra-low
priority.

Etienne


------------------------------
>
> Message: 3
> Date: Tue, 19 Mar 2013 10:37:56 -0300
> From: Hugo Drumond Jacob <hugo at jacob.eng.br>
> Subject: [Interest] QThread and sleep
> To: "interest at qt-project.org" <Interest at qt-project.org>
> Message-ID:
>         <
> CAOcVdpKd5gDzNs_Y-ZUsDsLfYrMzgiP68Q42JfyqtEHTZx+GjQ at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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 --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130319/51ea157a/attachment.html>


More information about the Interest mailing list