[Qt-interest] Multithreading - yield function

Patric userqt at gmail.com
Tue Sep 1 11:46:28 CEST 2009


The function works.

It just doesn't do what you expect it to do. You're expecting that another
thread gets woken up and run, while your thread is suspended for a long 
time.

That's not what happens. That only causes the current thread to give up its
processor time. Other threads may wake up, if they are ready for execution.
But the current thread will wake up too, very soon.

If you don't insert a synchronisation primitive, like a QWaitCondition or a
QSemaphore, there's no guarantee of how long your thread will get suspended
for. It may get suspended but then woken up again before the other thread
finishes execution.

If you want to program with threads, I recommend you read some books on the
subject first. It is *very* tricky to get it right. Programming threads 
when,
like you said, you don't have much experience, is a very daunting task.

---------------------------------------------------------------------------------------

I am confused. In my program I have two threads and whe I use yield on the 
second one (worker), the first one is not immidiately getting control. 
Howevever, when I use msleep(1) it is getting control and it's working like 
I expect "yield" to behave.
"Yields execution of the current thread to another runnable thread, if any. 
Note that the operating system decides to which thread to switch."

I'll take your advice and read some book about threads. It seems I don't 
know a lot of things on this subject...

Regards,
Patric



----- Original Message ----- 
From: "Thiago Macieira" <thiago.macieira at trolltech.com>
To: <qt-interest at trolltech.com>
Sent: Monday, August 31, 2009 11:37 PM
Subject: Re: [Qt-interest] Multithreading - yield function


> _______________________________________________
> 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