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);
///////////////////////////////////////////////////////////
이상.
'프로그래밍 > VC++ 개발 코딩' 카테고리의 다른 글
Windows IPC - Shared Memory Using DLL(공유 메모리) (0) | 2017.03.18 |
---|---|
w_char (TCHAR) 가변인수 만들기 (0) | 2016.04.29 |
바이트 정렬(Byte Alignment) pragma pack(1) (0) | 2015.11.20 |
DebugView 사용하기 (0) | 2015.09.21 |
프로그램 관리자 권한 주기 (0) | 2015.09.15 |