#include #include #include // for multithreading #include #include #include #include int totalthreads = 0; boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe! boost::mutex numthreads_mutex; // for changing value of numthreads boost::mutex threadid_mutex; // for manipulating thread_id using namespace std; void updatethread(int id) { { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout << " Thread:" << id << " running. " << std::endl; } // wait some secs boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); boost::thread::sleep(xt); { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout << " Thread:" << id << " Exiting. " << std::endl; } } main() { boost::xtime xt; int totdids=99999; // ******************************** // Main thread-spawning loop // for (int i=0; i<=totdids; i++) { totalthreads++; { boost::mutex::scoped_lock scoped_lock(io_mutex); std::cout << "MAIN:Thread:" << totalthreads << " being spawned... " << std::endl; } boost::thread t (boost::bind( updatethread, totalthreads) ); // wait some seconds boost::xtime_get(&xt, boost::TIME_UTC); xt.nsec += 100; boost::thread::sleep(xt); t.join (); } // end thread-spawning for loop std::cout << "Done." << std::endl; }