[Qt-interest] Waking-up a sleeping thread

Harry Sfougaris hsfougaris at gmail.com
Fri Jun 11 09:44:58 CEST 2010


I managed to avoid mutexes so far in my code as I don't need them and I was trying to avoid the overhead.
But it's a good suggestion: I hadn't realized wakeOne() would break the timer.

Thanks,
Harry

On 11 Ιουν 2010, at 10:40 π.μ., Mandeep Sandhu wrote:

> On Fri, Jun 11, 2010 at 12:52 PM, Harry Sfougaris <hsfougaris at gmail.com> wrote:
>> Is it possible to "wake-up" a thread, after calling sleep(secs) on it?
>> I have a thread that periodically does a job. I have the following pseudocode in the run() function of my thread:
>> ...
>> while (!stopped) {
>>        sleep( jobInterval);
>>        if (stopped) break;
>>        doLengthyTask();
>> }
>> ...
>> my thread deconstructor is
>> myThread::~myThread() {
>>    stopped = true;
>>    wait();
>> }
>> 
>> My problem is when the user tries to exit the app, the program will stall for up to jobInterval seconds, as the thread might be sleeping, and the deconstructor waits for it.
>> Is there another way to handle this?
> 
> Yes. I would use a QWaitCondition::wait(mutex, time). You can wake it
> up by calling wakeOne()/wakeAll() even while it's waiting for the
> timeout to occur.
> 
> HTH,
> -mandeep
> 
>> 
>> Thanks,
>> Harry
>> 
>> 
>> 
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>> 





More information about the Qt-interest-old mailing list