Document directory
- Thread startup
- Interruption Mechanism
- Thread Synchronization
- Thread group
Boost: Use of the thread Library
2009/11/26
Kagula
Http://blog.csdn.net/lee353086/article/details/4673790
Intended audience
This document assumes that the reader has several skills
[1] at least one multi-threaded Development Library has been used in C ++, with the concept of mutex and lock.
[2] familiar with C ++ development, able to compile and set boost: thread library in development tools.
Environment
[1] Visual Studio 2005/2008 with SP1
[2] boost1.39/1.40
Summary
This article describes how to use boost thread through examples. It consists of several parts: thread startup, interruption mechanism, thread synchronization, waiting for thread exit, and thread group.
Body thread startup
Threads can be started in the following three ways:
First, use the operator member function in the struct structure to start:
Struct callable
{
Void operator () {several lines of code are omitted here}
};
Several lines of code are omitted here
Callable X;
Boost: thread t (X );
Second, start the thread in the form of a non-member function
Void func (INT np)
{Several lines of code are omitted here
}
Several lines of code are omitted here
Boost: thread t (func, 123 );
Third, start the thread as a member function
# Include <boost/Bind. HPP>
Several lines of code are omitted here
Class testbind {
Public:
Void testfunc (int I)
{
Cout <"I =" <I <Endl;
}
};
Several lines of code are omitted here
Testbind TB;
Boost: thread t (boost: BIND (& testbind: testfunc, & TB, 100 ));
Interruption Mechanism
You can use the interrupt function of the thread object to notify the thread and interrupt is required. The thread can exit after it runs to the interruption point.
Example of interruption mechanism:
# Include "stdafx. H"
# Include <iostream>
# Include <boost/thread. HPP>
Using namespace STD;
Void F ()
{
For (INT I = 1; I <0x0fffffff; I ++)
{
If (I % 0 xffffff = 0)
{
Cout <"I =" <(I & 0x0f000000)> 24) <Endl;
Cout <"Boost: this_thread: interruption_requested () =" <boost: this_thread: interruption_requested () <Endl;
If (I & 0x0f000000)> 24) = 5)
{
Boost: this_thread: interruption_point ();
}
}
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Boost: thread t (f );
T. Interrupt ();
T. Join (); // wait until the thread ends.
Return 0;
}
T. Interrupt (); tells the T thread that interrupt is required now. Boost: this_thread: interruption_requested () to check whether the current thread has an interrupt request. If an interrupt request exists, the thread ends when it runs to interruption. Boost: this_thread: interruption_point (); is an interruption point. Interruption point has multiple forms and is commonly used in boost: this_thread: Sleep (boost: posix_time: seconds (5); when there is no interrupt request, this statement will keep the current thread sleep for five seconds. If interrupt exists
The requirement thread ends.
The following example shows how to ensure that the thread does not end when running to the interruption point:
# Include "stdafx. H"
# Include <iostream>
# Include <boost/thread. HPP>
Using namespace STD;
Void F ()
{
For (INT I = 1; I <0x0fffffff; I ++)
{
If (I % 0 xffffff = 0)
{
Cout <"I =" <(I & 0x0f000000)> 24) <Endl;
Cout <"Boost: this_thread: interruption_requested ()" <boost: this_thread: interruption_requested () <Endl;
If (I & 0x0f000000)> 24) = 5)
{
Boost: this_thread: disable_interruption di;
{
Boost: this_thread: interruption_point ();
}
}
}
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Boost: thread t (f );
T. Interrupt ();
T. Join (); // wait until the thread ends.
Return 0;
}
Note: The use of the boost: this_thread: disable_interruption statement prevents the interruption point in braces from interrupting the current thread.
Thread Synchronization
It takes a long time to get started with boost by providing multiple locks. Let's take a look at the following example of thread synchronization. I believe it is sufficient in most applications:
Example of using boost: mutex directly
Static boost: mutex G_m;
Several lines of code are omitted here
G_m.lock ();
Code to be locked
G_m.unlock ();
Several lines of code are omitted here
If (g_m.try_lock ())
{
Code to be locked
}
Several lines of code are omitted here
Example of using lock guard
# Include <iostream>
# Include <string>
# Include <boost/thread. HPP>
# Include <boost/thread/mutex. HPP>
# Include <boost/thread/locks. HPP>
Using namespace STD;
Static boost: mutex G_m;
Void F (string strname)
{
For (INT I = 1; I <0x0fffffff; I ++)
{
If (I % 0 xffffff = 0)
{
Boost: lock_guard <boost: mutex> lock (G_m );
Cout <"name =" <strname <"I =" <(I & 0x0f000000)> 24) <Endl;
}
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Boost: thread t (F, string ("Inuyasha "));
Boost: thread T2 (F, string ("kagula "));
Boost: thread T3 (F, string ("kikyou "));
{
Boost: lock_guard <boost: mutex> lock (G_m );
Cout <"thread id =" <t. get_id () <Endl;
}
T. Join ();
T2.join ();
T3.join ();
Return 0;
}
Example of using unique lock
# Include <iostream>
# Include <string>
# Include <boost/thread. HPP>
# Include <boost/thread/mutex. HPP>
# Include <boost/thread/locks. HPP>
Using namespace STD;
Static boost: mutex G_m;
Void F (string strname)
{
Cout <"thread name is" <strname <"--------------- begin" <Endl;
For (INT I = 1; I <0x0fffffff; I ++)
{
If (I % 0 xffffff = 0)
{
Boost: unique_lock <boost: mutex> lock (G_m );
Cout <"name =" <strname <"I =" <(I & 0x0f000000)> 24) <Endl;
Lock. Unlock ();
}
}
Cout <"thread name is" <strname <"--------------- end" <Endl;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Boost: thread t (F, string ("Inuyasha "));
Boost: thread T2 (F, string ("kagula "));
Boost: thread T3 (F, string ("kikyou "));
T. Join ();
T2.join ();
T3.join ();
Return 0;
}
Compared with lock_guard
[1] unique lock has the owns lock member function. You can check whether the function is currently locked.
[2] When constructing a unique lock, you can specify the boost: defer_lock_t parameter to postpone the lock until the unique lock instance calls the lock. Or use the following encoding method:
Boost: unique_lock <boost: mutex> lock (MUT, boost: defer_lock );
Boost: unique_lock <boost: mutex> lock2 (mut2, boost: defer_lock );
Boost: Lock (lock, lock2 );
[3] It can be used with conditoin_variable.
[4] provides the try lock function.
If there is a dependency between threads in the execution sequence, go to the boost official website and refer to the use of the condition variable. The description of conditon variables on the official website is easy to understand.
Note that using an inappropriate synchronization may consume more than 1/2 of the CPU computing power.
Thread group
Thread group usage example. The F function has been defined in the preceding example.
Int _ tmain (INT argc, _ tchar * argv [])
{
Boost: thread_group TG;
TG. add_thread (new boost: thread (F, string ("Inuyasha ")));
TG. add_thread (new boost: thread (F, string ("kagula ")));
TG. add_thread (new boost: thread (F, string ("kikyou ")));
TG. join_all ();
Return 0;
}
Reference Source
[1] www.boost.org