#include "test.h"
#include <windows.h>
volatile long m = 0;
HANDLE *eventH;
DWORD __stdcall fun(void *p)
{
Sleep(1000);
while(InterlockedExchange(&m, 1) != 0)
{
Sleep(0);
}
cout << *(int*)p << endl;
m = 0;
SetEvent(eventH[*(int*)p]);
return 0;
}
int main()
{
const unsigned int num = 64;
DWORD threadid[num];
HANDLE h[num];
eventH = new HANDLE[num];
unsigned int buffer[num];
for (int i = 0; i < num; ++i)
{
eventH[i] = CreateEvent(NULL, false, 0, NULL);
buffer[i] = i;
h[i] = CreateThread(NULL, 0, fun, &buffer[i], 0, &threadid[i]);
}
WaitForMultipleObjects(num, eventH, true, INFINITE);
WaitForMultipleObjects(num, h, true, INFINITE);
return 0;
}