通过这个简单的示例进行可以很快学习了解Event的用法.
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "iostream"
#include "process.h"
#define THREADCOUNT 10
HANDLE ghWriteEvent;
HANDLE ghMutex;
HANDLE ghThreads[THREADCOUNT];
DWORD WINAPI ThreadProc(LPVOID);
void CreateEventsAndThreads(void)
{
int i;
DWORD dwThreadID;
ghMutex = CreateMutex(NULL, FALSE, NULL);
ghWriteEvent = CreateEvent( NULL, FALSE,TRUE, TEXT("WriteEvent"));
if (ghWriteEvent == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
}
for(i = 0; i < THREADCOUNT; i++)
{
ghThreads[i] = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadID);
if (ghThreads[i] == NULL) {
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
}
}
void WriteToBuffer(VOID)
{
printf("Main thread writing to the shared buffer...\n");
if (! SetEvent(ghWriteEvent) ) {
printf("SetEvent failed (%d)\n", GetLastError());
return;
}
}
void CloseEvents() {
CloseHandle(ghWriteEvent);
}
void main()
{
DWORD dwWaitResult;
CreateEventsAndThreads();
//WriteToBuffer();
printf("Main thread waiting for threads to exit...\n");
dwWaitResult = WaitForMultipleObjects(THREADCOUNT, ghThreads, TRUE, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
printf("All threads ended, cleaning up for application exit...\n");
break;
default:
printf("WaitForMultipleObjects failed (%d)\n", GetLastError());
return;
}
CloseEvents();
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
DWORD dwWaitResult;
printf("Thread %d waiting for write event...\n", GetCurrentThreadId());
Sleep(100);
dwWaitResult = WaitForSingleObject(ghWriteEvent, INFINITE);
Sleep(100);
printf("Thread %d waiting for write event2...\n", GetCurrentThreadId());
dwWaitResult = WaitForSingleObject(ghWriteEvent, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
printf("Thread %d reading from buffer\n",
GetCurrentThreadId());
break;
default:
printf("Wait error (%d)\n", GetLastError());
return 0;
}
Sleep(100);
printf("Thread %d exiting\n", GetCurrentThreadId());
return 1;
}
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include "iostream"
#include "process.h"
#define THREADCOUNT 10
HANDLE ghWriteEvent;
HANDLE ghMutex;
HANDLE ghThreads[THREADCOUNT];
DWORD WINAPI ThreadProc(LPVOID);
void CreateEventsAndThreads(void)
{
int i;
DWORD dwThreadID;
ghMutex = CreateMutex(NULL, FALSE, NULL);
ghWriteEvent = CreateEvent( NULL, FALSE,TRUE, TEXT("WriteEvent"));
if (ghWriteEvent == NULL)
{
printf("CreateEvent failed (%d)\n", GetLastError());
return;
}
for(i = 0; i < THREADCOUNT; i++)
{
ghThreads[i] = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadID);
if (ghThreads[i] == NULL) {
printf("CreateThread failed (%d)\n", GetLastError());
return;
}
}
}
void WriteToBuffer(VOID)
{
printf("Main thread writing to the shared buffer...\n");
if (! SetEvent(ghWriteEvent) ) {
printf("SetEvent failed (%d)\n", GetLastError());
return;
}
}
void CloseEvents() {
CloseHandle(ghWriteEvent);
}
void main()
{
DWORD dwWaitResult;
CreateEventsAndThreads();
//WriteToBuffer();
printf("Main thread waiting for threads to exit...\n");
dwWaitResult = WaitForMultipleObjects(THREADCOUNT, ghThreads, TRUE, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
printf("All threads ended, cleaning up for application exit...\n");
break;
default:
printf("WaitForMultipleObjects failed (%d)\n", GetLastError());
return;
}
CloseEvents();
}
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
DWORD dwWaitResult;
printf("Thread %d waiting for write event...\n", GetCurrentThreadId());
Sleep(100);
dwWaitResult = WaitForSingleObject(ghWriteEvent, INFINITE);
Sleep(100);
printf("Thread %d waiting for write event2...\n", GetCurrentThreadId());
dwWaitResult = WaitForSingleObject(ghWriteEvent, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
printf("Thread %d reading from buffer\n",
GetCurrentThreadId());
break;
default:
printf("Wait error (%d)\n", GetLastError());
return 0;
}
Sleep(100);
printf("Thread %d exiting\n", GetCurrentThreadId());
return 1;
}