pop*_*lop 2 c++ winapi multithreading
我正在使用c ++中的winapi创建一个程序.该程序涉及两个线程.我使用CreateThread创建其中一个线程.问题是CreateThread在创建线程之前不会阻塞.这会导致问题,因为我在线程之间发送消息,并且线程在创建线程之前不会收到任何消息.怎么会解决这个问题.
使用CreateEvent来创建你让等待线程A.你线程B做的第一件事是信令事件的事件句柄.
struct thread_data {
/* ... */
HANDLE started_event;
};
Run Code Online (Sandbox Code Playgroud)
线程A:
/* We can create this on the stack. We wait for thread_B to
* copy it into its own stack before signalling the event. */
struct thread_data td;
td.started_event = CreateEvent(
NULL /* security attributes */,
FALSE /* manual reset (=NO) */,
FALSE /* initially signalled (=NO) */,
NULL /* name (=none) */ );
CreateThread(NULL, 0, thread_B, &td, 0, NULL);
WaitForSingleObject(td.started_event, INFINITE);
CloseHandle(td.started_event);
Run Code Online (Sandbox Code Playgroud)
线程B:
DWORD WINAPI thread_B(LPVOID data)
{
/* local copy of thread data */
thread_data td = *((thread_data*)data);
SetEvent(td.started_event);
/* ... */
}
Run Code Online (Sandbox Code Playgroud)