STL C++11 관련하여,

thread를 사용하고자 하는 경우 하기 처럼 지정하고 사용하면 된다.

 

 

예) Log 관련 thread를 만들고자 한다면...

.h 지정

#include <sstream>

 

 volatile bool     m_bAlive_thread_queueLogMsg;
 std::mutex      m_mutex_queueLogMsg;
 std::thread      m_thread_queueLogMsg; 
 void       m_fn_run_queueLogMsg_Proc();

 

 

 

.cpp 사용

 -. 종료하기

 // thread 안전 종료
 m_bAlive_thread_queueLogMsg = false;
 if (m_thread_queueLogMsg.joinable())
  m_thread_queueLogMsg.join();

 

 

 -. 생성하기

 // log receiver thread 생성
 m_bAlive_thread_queueLogMsg = true;
 m_thread_queueLogMsg = std::thread(&해당Class::m_fn_run_queueLogMsg_Proc, this);

 

 

 -. thread body

 while (m_bAlive_thread_queueLogMsg)
 {
  if (PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_REMOVE))

......

 

    {
     // mutext guard & insert queue
     std::lock_guard<std::mutex> lock(m_mutex_queueLogMsg);  // 지역 mutex 걸기.
     m_queueLogMsgContainer.push(stLogMsgInf);     
    }

 

Thread 안에서   Window Message를 사용하고자 한다.

이러한 경우 외부에서 PostThreadMessage를 사용하여야 한다.

std::thread::id 정보를 이용하여 threadid를 가져오는게 가능하다.

 

///////////////////////////////////////////////////////////

 auto myid = m_thread_queueLogMsg.get_id();
 std::stringstream ss;
 ss << myid;
 string mystring = ss.str();
 uint64_t test = std::stoull(ss.str());

DWORD dThreadId = (DWORD)test;

 PostThreadMessage(dThreadId, WM_USER, (WPARAM)xxx, (LPARAM)yyy);

///////////////////////////////////////////////////////////

 

이상.

 

 

 

+ Recent posts