乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      算法35(生產(chǎn)者線(xiàn)程和消費(fèi)者線(xiàn)程)

       白雪~~~ 2012-03-20

       

      /*
      生產(chǎn)者消費(fèi)者線(xiàn)程演示
      一個(gè)生產(chǎn)者線(xiàn)程將int類(lèi)型的數(shù)入列,一個(gè)消費(fèi)者線(xiàn)程將int類(lèi)型的數(shù)出列
      */
      #include <windows.h>
      #include <stdio.h>
      #include <process.h>
      #include <iostream>
      #include <queue>
      using namespace std;
      HANDLE ghSemaphore;   //信號(hào)量
      const int gMax = 100; //生產(chǎn)(消費(fèi))總數(shù)
      std::queue<int> q;   //生產(chǎn)入隊(duì),消費(fèi)出隊(duì)
      //生產(chǎn)者線(xiàn)程
      unsigned int __stdcall producerThread(void* pParam)
      {
       int n = 0;
       while(++n <= gMax)
       {
        //生產(chǎn)
        q.push(n);
        cout<<"produce "<<n<<endl;
        ReleaseSemaphore(ghSemaphore, 1, NULL); //增加信號(hào)量
        Sleep(300);//生產(chǎn)間隔的時(shí)間,可以和消費(fèi)間隔時(shí)間一起調(diào)節(jié)
       }
       _endthread(); //生產(chǎn)結(jié)束
       return 0;
      }
      //消費(fèi)者線(xiàn)程
      unsigned int __stdcall customerThread(void* pParam)
      {
       int n = gMax;
       while(n--)
       {
        WaitForSingleObject(ghSemaphore, 10000);
        //消費(fèi)
        q.pop();
        cout<<"custom  "<<q.front()<<endl;
        Sleep(500);//消費(fèi)間隔的時(shí)間,可以和生產(chǎn)間隔時(shí)間一起調(diào)節(jié)
       }
       //消費(fèi)結(jié)束
       CloseHandle(ghSemaphore);
       cout<<"working end."<<endl;
       _endthread();
       return 0;
      }
      void threadWorking()
      {
       ghSemaphore = CreateSemaphore(NULL, 0, gMax, NULL); //信號(hào)量來(lái)維護(hù)線(xiàn)程同步
       
       cout<<"working start."<<endl;
       unsigned threadID;
       HANDLE handles[2];
       handles[0] = (HANDLE)_beginthreadex(
                          NULL,
                          0,
                          producerThread,
                          nullptr,
                          0,
                          &threadID);
       handles[1] = (HANDLE)_beginthreadex(
                          NULL,
                          0,
                          customerThread,
                          nullptr,
                          0,
                          &threadID);
       WaitForMultipleObjects(2, handles, TRUE, INFINITE);
      }
      int main()
      {
       threadWorking();
       getchar();
       return 0;
      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多