您的位置:首页 > 其它

利用事件对象同步线程

2011-09-22 14:55 295 查看
#include <windows.h>
#include <stdio.h>

#define NUMTHREADS 3
#define BUFFER_SIZE 16
#define FOR_TIMES 5

HANDLE hEvent;
BYTE lpSharedBuffer[16] = {0};

void UseEvents(void);
DWORD WINAPI EventFunction(LPVOID lpParam);

int main(){
UseEvents();
}

void UseEvents(void){
HANDLE hThread;
hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
if(hEvent == NULL){
printf("CreateEvent failed.(%d)\n",GetLastError());
return;
}
hThread = CreateThread(NULL,0,EventFunction,NULL,0,NULL);
if(hThread == NULL){
printf("CreateThread failed.(%d)\n",GetLastError());
return;
}
Sleep(2000);
CopyMemory(lpSharedBuffer,"Event",strlen("Event"));
SetEvent(hEvent);
}

DWORD WINAPI EventFunction(LPVOID lpParam){
DWORD dwWaitResult;
dwWaitResult = WaitForSingleObject(hEvent,INFINITE);
if(dwWaitResult!=WAIT_OBJECT_0){
printf("Wait error:%d\n",GetLastError());
return 0;
}
printf("0%x",lpSharedBuffer);
system("pause");
if(!ResetEvent(hEvent)){
printf("SetEvent failed(%d)\n",GetLastError());
return 0;
}
return 1;
}


本文出自 “About:Blank H4cking” 博客,请务必保留此出处http://pnig0s1992.blog.51cto.com/393390/671313
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: