标签:als iostream void word creat blog 火车票 include else
##占位
#include <windows.h> #include <iostream> int tickets = 100; HANDLE hMutex; DWORD WINAPI FunProc1(LPVOID param) { while (TRUE) { WaitForSingleObject(hMutex, INFINITE); if (tickets > 0) { std::cout << "thread1 sell ticket:" << tickets-- << std::endl; } else break; ReleaseMutex(hMutex); } return 0; } DWORD WINAPI FunProc2(LPVOID param) { while (TRUE) { WaitForSingleObject(hMutex, INFINITE); if (tickets > 0) { std::cout << "thread2 sell ticket:" << tickets-- << std::endl; } else break; ReleaseMutex(hMutex); } return 0; } int main() { HANDLE hThread1, hThread2; hMutex = CreateMutex(NULL, FALSE, NULL);//创建互斥体的线程(也就是此主线程)不拥有 //互斥对象,操作系统会将计数置为0,设为有信号状态。 hThread1 = CreateThread(NULL, 0, FunProc1, NULL, 0, NULL); hThread2 = CreateThread(NULL, 0, FunProc2, NULL, 0, NULL); CloseHandle(hThread1); CloseHandle(hThread2); Sleep(4000); }
标签:als iostream void word creat blog 火车票 include else
原文地址:http://www.cnblogs.com/tinaluo/p/7710768.html