std::thread
阿新 • • 發佈:2018-06-21
ring list BE reset ipa serve lee 參數 static
std::thread為C++11的線程類,使用方法和boost接口一樣,非常方便。
C++11的std::thread解決了boost::thread中構成參數限制的問題。
#include <thread> // std::thread, std::thread::id, std::this_thread::get_id
#include <chrono> // std::chrono::seconds
void threadfun1(){}
void threadfun2(int iParam, std::string sParam){}
std::thread t1(threadfun1);
std::thread t2(threadfun2, 10, "abc");
std::shared_ptr<std::thread> m_routine;
1 m_routine.reset( new std::thread( [&]() 2 { 3 while (true) 4 { 5 std::this_thread::sleep_for(std::chrono::minutes(1)); 6 } 7 }) 8 ); 9 m_routine->detach();View Code
list< shared_ptr<thread> > m_listThreads; // 線程管理
1 auto ptrServerThread1 = shared_ptr<thread>(new thread(&SoapMgr::SoapServerThread, this, Enum_Service, g_config.iFSUPort)); 2 m_listThreads.push_back(ptrServerThread1); 3 static void SoapServerThread(LPVOID Param, shortView CodeSCServiceIndex, short port); 4 void SoapMgr::SoapServerThread(LPVOID Param, short SCServiceIndex, short port) 5 { 6 SoapMgr* pMgr = (SoapMgr*)Param; 7 std::this_thread::sleep_for(std::chrono::seconds(5)); 8 } 9 void SoapMgr::Exit() 10 { 11 m_bExit = true; 12 std::for_each(m_listThreads.begin(), m_listThreads.end(),[](shared_ptr<thread> ptr){ptr->join();}); 13 m_listThreads.clear(); 14 }
std::thread