Hi there,
at the end of this mail is a small program that starts three threads out of
a fourth one ("thread4"). thread1, 2 and 3 are not allowed to start
execution before a certain condition is met (here: go_ is set to true and
the threads are notified of the changed condition).
The threads increment a variable. Once this variable has reached a threshold
(here: 10), execution is supposed to stop. This is done by calling the
interrupt() function on each thread, which in turn catch the corresponding
exception and act on it.
All this seems to work nicely, except for the fact that the "master thread"
(thread4 in this example) does not seem to get any processing time over a
long period. Hence thread 1,2 and 3 count to a very high number before the
stop condition is met.
Here is the output of the program:
Starting threads
Going to sleep for 2 seconds in startAndStopThreads()
Hello world Nr. 0 from thread 1
Hello world Nr. 1 from thread 2
[...]
Hello world Nr. 6167 from thread 2
Hello world Nr. 6168 from thread 2
Sending interrupt
Received interrupt in thread 3
Received interrupt in thread 1
Received interrupt in thread 2
Done ...
Occasionally the counting can go on up to a few hundred thousand. This is on
a single-processor machine with OpenSUSE 11/64 bit, g++ 4.3.1 and a recent
trunk version of Boost 1.36 .
The situation is much better on a four-processor system with the same
operating system - the counter typically reaches only a few hundred there,
presumably as thread4 gets access to a time slice more quickly (but why
isn't it running permanently on a four processor machine - there are only 4
active threads ?).
Is there anything that can be done to improve the time slices allocated to
thread4 ? I am already calling the yield() function in thread1, 2 and 3.
Thanks and Best Regards,
Ruediger
/*********************************************************************/
#include <iostream>
#include