利用事件对象同步线程
2011-09-22 14:55
363 查看
#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
相关文章推荐
- 孙鑫VC学习笔记:第十六讲 (一) 利用事件对象实现线程间的同步
- 孙鑫VC学习笔记:第十六讲 利用事件对象实现线程间的同步
- Linux平台用C++实现事件对象,同步线程
- Linux平台用C++实现事件对象,同步线程(转)
- 利用事件同步,互斥线程
- 事件对象_打印字符串_2线程_同步
- Windows编程--线程和内核对象的同步-事件内核对象
- MFC_事件对象_打印字符串_2线程_同步
- Linux平台用C++实现事件对象,同步线程
- 线程与事件内核对象的同步
- 线程的同步互斥之事件对象(Event)
- 使用事件对象实现线程间的同步
- 事件对象 互斥对象对 线程的同步
- 用C++实现Win32事件对象,同步线程
- 用c++实现win32事件对象,同步线程
- 利用内核对象同步线程
- 利用内核对象同步线程
- Java线程同步:synchronized锁住的是代码还是对象
- 线程,事件,同步随笔
- Windows驱动开发之线程与同步事件