std::thread
Posted osbreak
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了std::thread相关的知识,希望对你有一定的参考价值。
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();
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, short SCServiceIndex, 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的主要内容,如果未能解决你的问题,请参考以下文章