本文共 1059 字,大约阅读时间需要 3 分钟。
- #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;
- }
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/671313,如需转载请自行联系原作者