Those readers who were trying to repeat all the examples by themselves, or those who were experimenting with threads must already be bored with writing the following code to launch and join threads:
#include <boost/thread.hpp> void some_function(); void sample() { boost::thread t1(&some_function); boost::thread t2(&some_function); boost::thread t3(&some_function); // ... t1.join(); t2.join(); t3.join(); }
Maybe there is a better way to do this?
We may manipulate a group of threads using the boost::thread_group
class.
- Construct a
boost::thread_group
variable:
#include <boost/thread.hpp> int main() { boost::thread_group threads;
- Create threads into the preceding variable:
// Launching 10 threads. for (unsigned i = 0; i < 10; ++i) { threads.create_thread(&some_function); }
- Now, you may...