boost::thread参考帮助文档:http://www.boost.org/doc/libs/1_57_0/doc/html/thread.html
1. Windows的多线程
参考博文:http://blog.youkuaiyun.com/morewindows/article/details/7421759
#include <process.h>
CRITICAL_SECTION g_cs;
void WinThread(LPVOID lP)
{
EnterCriticalSection(&g_cs); // 使用临界区保证互斥运行:另外还有事件Event, 信号量Semaphone, 互斥锁Mutex等
string *pstr = (string*)lP;
cout << "My WinThread: " << *pstr << endl << flush;
// do somthing
delete pstr;
LeaveCriticalSection(&g_cs);
}
unsigned int WINAPI WinThread2(void * lP) // 需要定义为__stdcall
{
EnterCriticalSection(&g_cs);
string *pstr = (string*)lP;
cout << "My Winthread2: " << *pstr << endl;
delete pstr;
LeaveCriticalSection(&g_cs);
_endthreadex(0);
return 0;
}
void TestWinThread()
{
InitializeCriticalSection(&g_cs);
HANDLE hThread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)WinThread, // 线程函数
new string("CreateThread"), // 函数参数: 需要新开辟内存,或静态、全局变量
0, NULL);
WaitForSingleObject(hThread, INFINITE); // 无限期等待线程执行结束
CloseHandle(hThread);
uintptr_t pTh = _beginthreadex(NULL, 0, WinThread2, new string("_beginthreadex"), 0, NULL);
WaitForSingleObject((HANDLE)pTh, 1000); // 等待线程执行完毕,超时时间1秒钟
cout << "_beginthreadex executed; " << endl;
}
运行结果:
2. boost::thread
boost::thread 的出现解决了函数参数需要静态、全局变量或者开辟新内存的问题。
博文参考:http://blog.youkuaiyun.com/iamnieo/article/details/2908621
看官方帮助文档:
struct callable
{
void operator()();
};
boost::thread copies_are_safe()
{
callable x;
return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK
boost::thread oops()
{
callable x;
return boost::thread(boost::ref(x));
} // x is destroyed, but the newly-created thread still has a reference
// this leads to undefined behaviour
我们可以:
struct MyCallable
{
MyCallable(int i) { m_iNo = i;}
void operator()()
{
for (int i = 0; i < 10; i++)
{
cout << "[" << m_iNo << "]报数" << i << endl;
}
}
int m_iNo;
};
void TestThread()
{
boost::thread t(MyCallable(0));
t.join(); // 主线程阻塞至t线程完成
return;
}
运行结果:
采用函数的形式,这里我们加入多线程互斥锁进行同步,是线程之间不相互影响。
boost::mutex io_mutex;
void BoostThreadFunc(int i)
{
boost::mutex::scoped_lock sl(io_mutex); // sl构造函数,调用io_mutex.lock()
MyCallable mc(i);
mc();
// sl析构函数, 调用io_mutext.unlock()
}
void TestThread()
{
boost::thread t1(BoostThreadFunc, 1);
boost::thread t2(BoostThreadFunc, 2);
#if 1
t1.join();
t2.join();
#else
boost::thread::native_handle_type handles[2];
handles[0] = t1.native_handle();
handles[1] = t2.native_handle();
WaitForMultipleObjects(2, handles, TRUE, INFINITE); // 等待t1,t2线程都执行结束
t1.detach();
t2.detach();
#endif
}
运行结果:
如果不加锁的话,执行的结果的不可预见的。
也支持多个参数:
class MyThreadPara
{
public:
MyThreadPara(string str) : m_strInfo(str) { }
void Print() {
cout << m_strInfo << endl;
}
private:
string m_strInfo;
};
void BoostThreadClassParaFunc(int i, MyThreadPara para1, MyThreadPara para2) // 最多9个
{
boost::mutex::scoped_lock sl(io_mutex); // sl构造函数,调用io_mutex.lock()
cout << "[" << i << "]" << endl;
para1.Print();
para2.Print();
// sl析构函数, 调用io_mutext.unlock()
}
void TestThread()
{
boost::thread t3(BoostThreadClassParaFunc, 3, MyThreadPara(string("FirstClassPara")), MyThreadPara(string("SecondClassPara")));
t3.join();
}
执行结果: