概要
通过实例介绍boost thread的使用方式,本文主要由线程启动、Interruption机制、线程同步、等待线程退出、Thread Group几个部份组成。
正文
线程启动
线程可以从以下三种方式启动:
第一种用struct结构的operator成员函数启动:
struct callable { void operator()() { 这里略去若干行代码 } }; 这里略去若干行代码 Callable x; Boost::thread t(x);
void func(int nP) { 这里略去若干行代码 } 这里略去若干行代码 Boost::thread t(func,123);
#include <boost/bind.hpp> 这里略去若干行代码 class testBind{ public: void testFunc(int i) { cout<<”i=”<<i<<endl; } }; 这里略去若干行代码 testBind tb; boost::thread t(boost::bind(&testBind::testFunc,&tb,100));
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std; void f() { for(int i=1;i<0x0fffffff;i++) { if(i%0xffffff==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(); //等待线程结束 return 0; }
#include "stdafx.h" #include <iostream> #include <boost/thread.hpp> using namespace std; void f() { for(int i=1;i<0x0fffffff;i++) { if(i%0xffffff==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(); //等待线程结束 return 0; }
线程同步
Boost提供了多种lock导致上手需要较长时间,还是看下面线程同步的例子比较简单,相信在多数应用中足够:
直接使用boost::mutex的例子
static boost::mutex g_m; 这里略去若干行代码 g_m.lock(); 需要锁定的代码 g_m.unlock(); 这里略去若干行代码 if(g_m.try_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) { for(int i=1;i<0x0fffffff;i++) { if(i%0xffffff==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; }
#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%0xffffff==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; }
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; }