[Qt-interest] Multithreading - yield function
Sean Harmer
sean.harmer at maps-technology.com
Tue Sep 1 12:13:40 CEST 2009
Hi,
On Tuesday 01 Sep 2009 10:56:52 Patric wrote:
> Also when I'm debugging and it's clear that another thread is waiting for
> execution, after yield it's just not jumping to the other thread.
How do you *know* the other thread is waiting for execution? This is up to the
kernel's scheduler to decide not you (unless you use explicit synchronisation
methods).
> >It may get suspended but then woken up again before the other thread
> >finishes execution.
>
> you are telling me that if I have two threads that are running, I really
> don't know when the second one will start executing in the middle of the
> code ? And stop also.
Correct. This is why things like QMutex and QSemaphore exist. Please read up
on their use. It is also why multi-threaded programming is hard.
> This don't sound very cool.
It is perfectly reasonable but you need to understand it. Consider the case of
a dual core system. In this case *both* of your threads could be running at
exactly the same time - one on each core. However even in this case the kernel
has to share the CPU cores and other resources between your applications
threads and all other applications and their threads and the kernel's own
threads. Not to mention respond to interrupt events.
Just let the kernel do its job. It is written by very good people who know
what they are doing. If you need to have some sort of specific synchronisation
between your threads then the yield function is not the way to go as this is
just a *hint* to the kernel. To force synchronisation look at QMutex and
QWaitCondition for example.
Good luck,
Sean
More information about the Qt-interest-old
mailing list