boost::thread_group
#define g_UserManager Singleton<UserManager>
#define g_MemDBManager Singleton<MemDBManager>::GetSingleton()
#define g_ProxyManager Singleton<ProxyManager>::GetSingleton()
#define CONV_MSEC2USEC(x) ( (x) * 1000 )
#define CONV_SEC2MSEC(x) ( (x) * 1000 )
#define CONV_SEC2USEC(x) ( CONV_SEC2MSEC(x) * 1000 )
class UserManager
{
public:
UserManager() {}
virtual ~UserManager() {}
void Run() { while (m_runFlag){ sleep(CONV_MSEC2USEC(1)); } }
void SetRunFlag(bool flag) { m_runFlag = flag; }
private:
bool m_runFlag = true;
}
class MemDBManager
{
public:
MemDBManager() {}
virtual ~MemDBManager() {}
void run_service_reconnect(int index, int intervalSec) { while (m_runFlag){ sleep(intervalSec); } }
void SetRunFlag(bool flag) { m_runFlag = flag; }
private:
bool m_runFlag = true;
}
class ProxyManager
{
public:
ProxyManager() {}
virtual ~ProxyManager() {}
void run(int index) { ... }
}
void ServiceMain::Run()
{
//logInfo("===========================================");
//logInfo("= Service Running");
//logInfo("===========================================");
boost::thread_group t_group;
std::list<boost::thread*> threadList;
//!< create_thread 함수 호출 시 스레드가 생성되어 thread_group에 귀속.
//!< 스레드 생성할 때마다 io_service 변수를 넘겨준 후 io_service의 post() 통해 bind 함수 넘겨주면,
//!< 해당 함수가 스레드 풀 중에서 남는 스레드를 선택하여 작업을 수행.
//!< bind : 특정 함수를 호출할 때 그 매개변수 값을 함께 넘겨주기 위해 사용.
threadList.push_back(t_group.create_thread(boost::bind(&UserManager::Run, &g_UserManager)));
threadList.push_back(t_group.create_thread(boost::bind(&MemDBManager::run_service_reconnect,
&g_MemDBManager, 0, CONV_SEC2USEC(10))));
threadList.push_back(t_group.create_thread(boost::bind(&ProxyManager::run, &g_ProxyManager, index++)));
threadList.push_back(t_group.create_thread(boost::bind(&ProxyManager::run, &g_ProxyManager, index++)));
//!< 스레드 종료시 까지 대기
t_group.join_all();
//!< Remove
for(std::list<boost::thread*>::iterator itor = threadList.begin(); itor != threadList.end(); ++itor)
{
t_group.remove_thread(*itor);
}
//logInfo("===========================================");
//logInfo("= Service is stopped");
//logInfo("===========================================");
}
출처 : [C/C++] Boost 주요 기능 정리 : 네이버 블로그 (naver.com)