Boost: thread has two constructors:
(1) thread (): constructs a thread object that represents the current execution thread;
(2) Explicit thread (const boost: function0 <void> & threadfunc ):
Boost: function0 <void> can be simply described as: a function with No Response (return void) and no parameter. The function here can also be a function consisting of a class overload operator (). The constructor imports a function object instead of a function pointer, such a class with general function features can also be passed in as a parameter, as shown in the following example.
Method 1: the simplest method
# Include <boost/thread. HPP>
# Include <iostream>
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Int main (INT argc, char * argv [])
{
Boost: thread thrd (& Hello );
Thrd. Join ();
Return 0;
}
Method 2: Use a complex type object as a parameter to create a thread:
# Include <boost/thread. HPP>
# Include <boost/thread/mutex. HPP>
# Include <iostream>
Boost: mutex io_mutex;
Struct count
{
Count (int id): ID (ID ){}
Void operator ()()
{
For (INT I = 0; I <10; ++ I)
{
Boost: mutex: scoped_lock
Lock (io_mutex );
STD: cout <id <":"
<I <STD: Endl;
}
}
Int ID;
};
Int main (INT argc, char * argv [])
{
Boost: thread thrd1 (count (1 ));
Boost: thread thrd2 (count (2 ));
Thrd1.join ();
Thrd2.join ();
Return 0;
}
Method 3: Create a thread in the class;
(1) Class internal static method to start the thread
# Include <boost/thread. HPP>
# Include <iostream>
Class helloworld
{
Public:
Static void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Static void start ()
{
Boost: thread thrd (Hello );
Thrd. Join ();
}
};
Int main (INT argc, char * argv [])
{
Helloworld: Start ();
Return 0;
}
Here, both the START () and hello () methods must be static methods.
(2) If the START () and hello () methods are not required to be static, use the following method to create a thread:
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
# Include <iostream>
Class helloworld
{
Public:
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Void start ()
{
Boost: function0 <void> F = boost: BIND (& helloworld: Hello, this );
Boost: thread thrd (f );
Thrd. Join ();
}
};
Int main (INT argc, char * argv [])
{
Helloworld hello;
Hello. Start ();
Return 0;
}
(3) create a thread in singleton mode:
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
# Include <iostream>
Class helloworld
{
Public:
Void Hello ()
{
STD: cout <
"Hello world, I ''m a thread! "
<STD: Endl;
}
Static void start ()
{
Boost: thread thrd (boost: bind
(& Helloworld: Hello, & helloworld: getinstance ()));
Thrd. Join ();
}
Static helloworld & getinstance ()
{
If (! Instance)
Instance = new helloworld;
Return * instance;
}
PRIVATE:
Helloworld (){}
Static helloworld * instance;
};
Helloworld * helloworld: instance = 0;
Int main (INT argc, char * argv [])
{
Helloworld: Start ();
Return 0;
}
Method 4: Use class internal functions to create threads outside the class;
# Include <boost/thread. HPP>
# Include <boost/Bind. HPP>
# Include <string>
# Include <iostream>
Class helloworld
{
Public:
Void Hello (const STD: string & Str)
{
STD: cout <STR <STD: Endl;
}
};
Int main (INT argc, char * argv [])
{
Helloworld OBJ;
Boost: thread thrd (boost: BIND (& helloworld: Hello, & OBJ, "Hello
World, I'm a thread! "));
Thrd. Join ();
Return 0;
}
If the function to be bound by a thread has parameters, use boost: bind. For example, if you want to use boost: thread to create a thread to execute the function: void F (int I), it is wrong to write: boost: thread thrd (f, the thread constructor accepts a type with no parameters and the return type is void. If the parameter I value is not provided, the thread constructor cannot run. In this case, you can write: boost :: thread thrd (boost: BIND (F, 1 )). Basically, the binding issues involving function parameters are boost: thread, boost: function, and boost: bind.