c++ - boost thread while all thread not completed print something -


need know

boost::thread_group tgroup; 

loop 10 times

tgroup.create_thread( boost::bind( &c , 2, 2, ) ) 

<==

tgroup.join_all() 

what can @ <== location above continuously print number of threads have completed there jobs

you can use atomic counter: see live on coliru

#include <boost/thread/thread.hpp> #include <boost/atomic.hpp>  static boost::atomic_int running_count(20);  static void worker(boost::chrono::milliseconds effort)  {      boost::this_thread::sleep_for(effort);     --running_count; }  int main() {     boost::thread_group tg;      (int = 0, count = running_count; < count; ++i) // count protects against data race!         tg.create_thread(boost::bind(worker, boost::chrono::milliseconds(i*50)));      while (running_count > 0)     {         std::cout << "monitoring threads: " << running_count << " running\n";         boost::this_thread::sleep_for(boost::chrono::milliseconds(100));     }      tg.join_all(); } 

example output:

monitoring threads: 19 running monitoring threads: 17 running monitoring threads: 15 running monitoring threads: 13 running monitoring threads: 11 running monitoring threads: 9 running monitoring threads: 7 running monitoring threads: 5 running monitoring threads: 3 running monitoring threads: 1 running 

another way use semaphore


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -