zoukankan      html  css  js  c++  java
  • 16.1.4火车站售票系统模拟程序(Event)

    #include<iostream>
    #include<Windows.h>
    using namespace std;

    DWORD WINAPI ThreadProc1(LPVOID lpParameter);
    DWORD WINAPI ThreadProc2(LPVOID lpParameter);
    HANDLE hEvent;
    int tickets = 100;

    int main()
    {
     hEvent = CreateEvent(NULL,FALSE,TRUE,NULL);
     HANDLE hThread1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
     HANDLE hThread2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
     DWORD nCount = 2;
     HANDLE* lpHandles = new HANDLE[nCount];
     lpHandles[0] = hThread1;
     lpHandles[1] = hThread2;
     WaitForMultipleObjects(nCount,lpHandles,TRUE,INFINITE);
     CloseHandle(hThread2);
     CloseHandle(hThread1);
     CloseHandle(hEvent);
     return 0;
    }

    DWORD WINAPI ThreadProc1(LPVOID lpParameter)
    {
     while(true)
     {
      WaitForSingleObject(hEvent,INFINITE);
      if(tickets > 0)
      {
       cout<<"ThreadProc1 sells "<<tickets--<<endl;
       SetEvent(hEvent);
      }
      else
      {
       SetEvent(hEvent);
       break;
      }
     }
     return 0;
    }

    DWORD WINAPI ThreadProc2(LPVOID lpParameter)
    {
     while(true)
     {
      WaitForSingleObject(hEvent,INFINITE);
      if(tickets > 0)
      {
       cout<<"ThreadProc2 sells "<<tickets--<<endl;
       SetEvent(hEvent);
      }
      else
      {
       SetEvent(hEvent);
       break;
      }
     }
     return 0;
    }

  • 相关阅读:
    操作系统、存储介质以及电信行业单位换算差异
    Luogu P1659 [国家集训队]拉拉队排练
    AC自动机
    KMP
    Luogu P1470 最长前缀 Longest Prefix
    Luogu P2292 [HNOI2004]L语言
    Manacher算法
    字典(Trie)树
    逆序对
    vs
  • 原文地址:https://www.cnblogs.com/BeyondTechnology/p/1813095.html
Copyright © 2011-2022 走看看