Windows事件对象简单复习

想看孙鑫的书上写到的。 做个笔记。

 

#include <Windows.h> #include <stdio.h> #include <iostream> #include <vector> #include <algorithm> DWORD WINAPI ThreadProc1(LPVOID lpParam); DWORD WINAPI ThreadProc2(LPVOID lpParam); int g_tickets = 100; HANDLE g_hEvent; std::vector<int> v1; std::vector<int> v2; std::vector<int> intersection; int main() { HANDLE hThread1 = ::CreateThread(NULL, 0, ThreadProc1, 0, 0, NULL); HANDLE hThread2 = ::CreateThread(NULL, 0, ThreadProc2, 0, 0, NULL); ::CloseHandle(hThread1); ::CloseHandle(hThread2); g_hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL); ::SetEvent(g_hEvent); Sleep(3000); ::CloseHandle(g_hEvent); // sort sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); // 交集 set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), intersection.begin()); system("PAUSE"); return 0; } DWORD WINAPI ThreadProc1(LPVOID lpParam) { while (1) { ::WaitForSingleObject(g_hEvent, INFINITE); if (g_tickets > 0) { v1.push_back(g_tickets--); printf("thread1 sell 1 ticket, left: %d tickets./n", g_tickets); } else { break; } ::SetEvent(g_hEvent); } return 0; } DWORD WINAPI ThreadProc2(LPVOID lpParam) { while (1) { ::WaitForSingleObject(g_hEvent, INFINITE); if (g_tickets > 0) { v2.push_back(g_tickets--); printf("thread2 sell 1 ticket, left: %d tickets./n", g_tickets); } else { break; } ::SetEvent(g_hEvent); } return 0; }

你可能感兴趣的:(thread,windows,null,System,winapi)