@@ -41,9 +41,9 @@ class worker: public pthread::runnable {
4141 {
4242 pthread::lock_guard<pthread::mutex> lck (mtx);
4343
44- message (" running worker class. wait max 20s for condition to be signaled" );
44+ message (" running worker class. wait max 2s for condition to be signaled" );
4545 bool stop_waiting = true ;
46- auto delay = 20 * 1000 ;
46+ auto delay = _sleep ;
4747 while ( ! (stop_waiting = (counter >= 10000 )) && (condition.wait_for (mtx, delay) == pthread::cv_status::no_timeout)){
4848 delay = -1 ;
4949 }
@@ -54,7 +54,8 @@ class worker: public pthread::runnable {
5454 }
5555 }
5656
57- pthread::this_thread::sleep (_sleep);
57+ message (" worker sleeping" );
58+ pthread::this_thread::sleep (200 );
5859 message (" worker class is ending" );
5960 };
6061
@@ -75,17 +76,27 @@ int main(int argc, const char * argv[]) {
7576 std::auto_ptr<pthread::thread> pt0 (new pthread::thread (w));
7677 pt0->join ();
7778
78- std::list<pthread::thread> threads;
79+ std::list<std::auto_ptr<pthread::thread>> threads;
80+ for (auto x = 20 ; x > 0 ; x--){
81+ threads.push_back (std::auto_ptr<pthread::thread>(new pthread::thread (w)));
82+ }
7983// threads.push_back(pthread::thread(w));
8084// threads.push_back(pthread::thread(w));
8185// threads.push_back(pthread::thread(w));
8286// threads.push_back(pthread::thread(worker("herbert's worker")));
8387
88+ message (" increment counter" );
89+ for ( auto x = 20000 ; x > 0 ; x--){
90+ pthread::lock_guard<pthread::mutex> lck (mtx);
91+ counter++ ;
92+ condition.notify_one ();
93+ }
8494 message (" main is waiting for threads to finish" );
8595
86- std::list<pthread::thread>::iterator iterator;
96+ std::list<std::auto_ptr< pthread::thread> >::iterator iterator;
8797 for (iterator = threads.begin (); iterator != threads.end (); iterator++){
88- iterator->join ();
98+ message (" join a thread (loop)" );
99+ (*iterator)->join ();
89100 }
90101
91102 message ( " end reached" );
0 commit comments