|
Boost : |
From: awite2_at_[hidden]
Date: 2003-04-15 16:47:23
Dear Readers,
Is this a general problem with threads or is the reason in the boost_thread
library?
==> I expected that both threads are executed evenly (see example below),
but one is executed more often.
Problem:
Having a program with 5 to 10 threads, I experienced that 1 thread was never
activated, if I set certain boost::sleep() values to each thread.
Therefore I tried the following short program (now with
boost::sleep(boost::xtime) instead of ::sleep(), and a recursive_timed_mutex
instead of a recursive_mutex).
But the result is similar:
The "fast" thread is run about 700 times, the "slow" thread is run 1 time,
and the main loop isn't executed much, as otherwise the "fast" thread would
not have reached 700. If I run it much longer, than the "slow" thread
becomes faster at some point than the "fast".
(I used linux 2.4.10, pthreads).
////////// Start of Example
#include <iostream.h>
#include <stdlib.h>
#include <pthread.h>
#include <boost/thread.hpp>
boost::recursive_mutex M;
int iFast = 0;
int iSlow = 0;
void FastThread()
{
while(1)
{
iFast ++;
cout << "Enter Fast " << iFast<< " : " << iSlow << endl;
{
boost::recursive_mutex::scoped_lock L(M);
}
cout << "Leave Fast" << endl;
}
}
void SlowThread()
{
while(1)
{
iSlow++;
cout << "Enter Slow " << iFast<< " : " << iSlow << endl;
{
boost::recursive_mutex::scoped_lock L(M);
}
}
}
int main(int argc, char *argv[])
{
cout << "Start" << endl;
boost::thread_group thrds;
thrds.create_thread(&FastThread);
thrds.create_thread(&SlowThread);
while((iFast < 10) && (iSlow < 10))
{
cout << "MAIN: Fast : Slow = "<< iFast<< " : " << iSlow << endl;
}
cout << "End" << endl;
return EXIT_SUCCESS;
}
/////////End of Example
Greetings,
Ernst
********************************************
Awite Bioenergie
Grepmeier & Murnleitner GbR
Angerstraße 9
D-85416 Langenbach-Niederhummel
Dr.-Ing. Ernst Murnleitner
http://awite.com
email: mur_at_[hidden]
Tel. +49-8761-334979
Fax. +49-8761-334981
********************************************
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk