Created
October 29, 2012 09:13
-
-
Save chenfjit/3972526 to your computer and use it in GitHub Desktop.
win32 event相关
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIN32_LEAN_AND_MEAN | |
| #include <cstdio> | |
| #include <cstdlib> | |
| #include <Windows.h> | |
| HANDLE g_event; | |
| DWORD WINAPI ThreadFunc(LPVOID); | |
| int main(int argc, char **argv) | |
| { | |
| g_event = CreateEvent(NULL, TRUE, FALSE, NULL); | |
| HANDLE hThrd = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL); | |
| Sleep(1000); | |
| printf("Going to set event\n"); | |
| SetEvent(g_event); | |
| WaitForSingleObject(hThrd, INFINITE); | |
| return 0; | |
| } | |
| DWORD WINAPI ThreadFunc(LPVOID) | |
| { | |
| printf("Enter sub thread\n"); | |
| while (1) { | |
| WaitForSingleObject(g_event, INFINITE); | |
| printf("Sub thread got event\n"); | |
| //ResetEvent(g_event); // 若没有这一句,则WaitForSingleObject会一直成功,说明手动触发event后状态是保持的 | |
| } | |
| return (DWORD)0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment