1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include "stdafx.h"
#include <windows.h>
HANDLE hThread[2];
HANDLE hEvent;
BOOL ModeManualReset = FALSE;
DWORD WINAPI _start(LPVOID lpParameter)
{
DWORD id = (DWORD)lpParameter;
for(;;)
{
printf("Thread %d is waiting...\n", id);
WaitForSingleObject(hEvent, INFINITE);
ResetEvent(hEvent);
printf("Thread %d is released\n", id);
}
}
int _tmain(int /*argc*/, _TCHAR* /*argv*/[])
{
// create the event
printf("ManualReset=%s\n", ModeManualReset== TRUE ? "TRUE" : "FALSE");
hEvent = CreateEvent(NULL, ModeManualReset, FALSE, NULL);
// create 3 thread
DWORD id;
HANDLE h = CreateThread(NULL, 0, _start, (void *)5, 0, &id);
hThread[0] = h;
h = CreateThread(NULL, 0, _start, (void *)6, 0, &id);
hThread[1] = h;
printf("Main thread is sleeping...\n");
Sleep(5000);
printf("Main thread is signaling the event...\n");
SetEvent(hEvent);
printf("Main thread is waiting...\n");
WaitForMultipleObjects(sizeof(hThread) / sizeof(hThread[0]), hThread, TRUE, INFINITE);
printf("Main thread is released\n");
return 0;
} |
Partager