#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
Using namespace boost;
Using namespace std;
void runchild (const int n
{
cout << "I am the first << n << "Child Threads" << endl Sleep cout << "process" < Span class= "sy1" ><< n << "exit" << endl /span>
int main (int argc, Char** argv)
{
int num Thread_group threads
if (argc < 2)
{
cout << "Please provide a parameter to generate the number of threads" << Endl;
Exit(-1);
}
num = atoi(argv[1]);
cout<<"I am the main program, and I am ready to produce"<< num<<"Child Threads"<< Endl;
For(int I=0; I< num; i++)
{
Threads. Create_thread&runchild, I }
cout < < "I am the main program, I am waiting for the child thread to run the end" << endl;
Threads. join_all ( return 0 /span>
Compile & Test (I tested it under my Ubuntu)
> g++ Threadgroup.cc-lboost_thread
>./a.out 3
If you compile under FREEBSD4, if you use pthread as the thread implementation, you need to make it clear that Pthread uses the line libraries, and that the default template depth does not seem to meet the boost demand. Need to be added at compile time:
-ftemplate-depth-20-boost
Using the thread pool in boost