[Qt-interest] Multithreading - yield function

Thomas Dähling t.daehling at googlemail.com
Mon Aug 31 22:46:55 CEST 2009


Someone please correct me if this is inherently wrong, but this is how I 
understood and used yielding so far ;-)

Roughly speaking, yield() is useful in a few situations only, and mainly 
used to tell the operating system: "Hey scheduler, I am done with the 
work that I am supposed to do, you might want to give someone else 
execution time now, if you think there is someone who needs it more than 
me".

So a typical use-case would be a system where you want to process at 
least X iterations of something within a thread (note: this is of course 
no guaranteed unless you provide some sort of blocking mechanism). There 
might be situations where said thread gets a time slice assigned to do 
X+N iterations, in that case a yield() after X iterations might be 
useful to return not-necessarily-required computation time to the 
scheduler. However, there is no guarantee that it will actually make 
another thread run - and of course there is no guarantee that you will 
get the X iterations done (since both parameters are almost completely 
out of your control).

/Thomas

Patric schrieb:
> I will consider that, I don't have much experience with threads.
> 
> One thing I don't understand, why there would be a function like yield that 
> don't always work. What is the reason such a function to exist if it's not 
> good to use it.
> Sorry if my questions are obvious or something like that. :)
> 
> Regards,
> Patric
> 
> 
> ----- Original Message ----- 
> From: "Andreas Pakulat" <apaku at gmx.de>
> To: <qt-interest at trolltech.com>
> Sent: Monday, August 31, 2009 9:42 PM
> Subject: Re: [Qt-interest] Multithreading - yield function
> 
> 
>> On 31.08.09 21:09:12, Patric wrote:
>>> And how do you know that it didn't yield?
>>>
>>> Yielding execution means allowing other threads to execute, while the
>>> calling
>>> thread is suspended. Eventually, however, the calling thread will resume
>>> working. So the "some other code" line *will* be executed, eventually.
>>>
>>> Anyway, whether the yielding actually does anything depends on the 
>>> operating
>>> system and the scheduling priorities of the runnable threads. Like Thomas
>>> said, if there are no threads waiting to be executed, there will be no
>>> yielding. But even if there are other threads waiting, they may be of 
>>> lower
>>> priority, so yielding will do nothing. You may have to exhaust your
>>> timeslice
>>> instead.
>>>
>>> Finally, why do you need to yield? It's just a hint to the operating 
>>> system.
>>> It may decide to ignore it.
>>>
>>>
>>> ----------------------------------------------------------------------------------
>>> Hi Thiago,
>>> I'm using the debugger with multiple breakpoints and because of that I 
>>> know
>>> if it's yielding or not. By default all threads priorities should be 
>>> equal.
>>> I'm sure there is one thread that is waiting.
>>>
>>> I need to yield it because my logic requires it. I'm using queued
>>> connections here and it's very important to know that the destination 
>>> slot
>>> is emitted before the thread continues.
>> Then either don't use queued connections but direct ones, or use the
>> usual thread-communication mechanisms to make your thread stop at that
>> point until the receiving thread is done with the slot. You could do
>> this for example with a QWaitCondition.
>>
>> Andreas
>>
>> -- 
>> You will become rich and famous unless you don't.
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
> 
> _______________________________________________
> 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