boost线程之类成员函数
生活随笔
收集整理的這篇文章主要介紹了
boost线程之类成员函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,是準備工作,包含頭文件
#include <iostream> #include <boost/bind.hpp> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp>using namespace std;//線程休眠,毫秒級 #define BOOST_SLEEP(n) boost::thread::sleep(boost::get_system_time()+boost::posix_time::millisec(n))然后,創建一個簡單的類CTest,
class CTest { public:CTest(){};~CTest(){};void Func();static void FuncThread(void *pData); };void CTest::Func() {cout<<"here is CTest::Func this="<<this<<endl;boost::thread thTest(boost::bind(CTest::FuncThread,this));thTest.join(); } void CTest::FuncThread(void *pData) {CTest *pOwner = (CTest*)pData;while(1){cout<<"here is CTest::FuncThread running. this="<<pOwner<<endl;BOOST_SLEEP(1000);} }最后,測試一下
int _tmain(int argc, _TCHAR* argv[]) {CTest test;test.Func();return 0; }關鍵點在于,作為線程的成員函數,必須是靜態的。
測試第二下,在類外部調用類成員函數產生線程
int _tmain(int argc, _TCHAR* argv[]) {CTest test;boost::thread thTest(boost::bind(CTest::FuncThread,&test));thTest.join();return 0;}轉載于:https://www.cnblogs.com/ttmoon/p/7658224.html
總結
以上是生活随笔為你收集整理的boost线程之类成员函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 进入、退出命令行的快捷键
- 下一篇: 密码字典收集-