<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      基于Blocking queue的生產消費模型

      file:

      BlockQueue.hpp:

      #pragma once
      #include<pthread.h>
      #include<cassert>
      #include<queue>
      const int maxsize=5;
      template<class T>
      class BlockQueue{
      public:
          BlockQueue(){
              int n;
              n=pthread_mutex_init(&m_mutex,nullptr);
              assert(n==0);
              n=pthread_cond_init(&m_cpond,nullptr);
              assert(n==0);
              n=pthread_cond_init(&m_ppond,nullptr);
              assert(n==0);
              (void)n;
          }
          ~BlockQueue(){
              pthread_mutex_destroy(&m_mutex);
              pthread_cond_destroy(&m_cpond);
              pthread_cond_destroy(&m_ppond);
          }
          void push(const T& task){
              pthread_mutex_lock(&m_mutex);
              while(is_full()){
                  pthread_cond_wait(&m_ppond,&m_mutex);
              }
              m_q.push(task);
              pthread_cond_signal(&m_cpond);
              pthread_mutex_unlock(&m_mutex);
          }
      
          void pop(T &task){
              pthread_mutex_lock(&m_mutex);
              while(is_empty()){
                  pthread_cond_wait(&m_cpond,&m_mutex);
              }
              task=m_q.front();
              m_q.pop();
              pthread_cond_signal(&m_ppond);
              pthread_mutex_unlock(&m_mutex);
          }
      private:
          bool is_empty(){
              return m_q.size()==0;
          }
          bool is_full(){
              return m_q.size()==maxsize;
          }
      private:
          std::queue<T> m_q;
          pthread_mutex_t m_mutex;
          pthread_cond_t m_cpond,m_ppond;
      };
      

      Task.hpp:

      #pragma once
      #include<functional>
      class Task{
      public:
          using func_t=std::function<int(int,int)>;
          Task(){}
          Task(const int &x,const int &y,func_t func):m_x(x),m_y(y),m_callback(func){}
          ~Task(){}
          int operator()(){
              return m_callback(m_x,m_y);
          }
      private:
          func_t m_callback;
          int m_x;
          int m_y;
      };
      

      MainCp.cpp:

      #include "BlockQueue.hpp"
      #include "Task.hpp"
      #include<ctime>
      #include<unistd.h>
      #include<iostream>
      using func_t=std::function<int(int,int)>;
      std::vector<func_t> fs;
      int add(int x,int y){
          return x+y;
      }
      
      int divid(int x,int y){
          return x/y;
      }
      int mul(int x,int y){
          return x*y;
      }
      void *comsumer(void *args){
          BlockQueue<Task> *bq=static_cast<BlockQueue<Task>*>(args);
          while(true){
              Task t;
              bq->pop(t);
              std::cout<<"消費數據:"<<t()<<std::endl;
              sleep(1);
          }
      }
      void *productor(void *args){
          BlockQueue<Task> *bq=static_cast<BlockQueue<Task>*>(args);
          while(true){
              int x=rand()%10+1;
              int y=rand()%10+1;
              int taskid=rand()%3;
              bq->push(Task(x,y,fs[taskid]));
              std::cout<<"生產數據:"<<x<<' '<<y<<' '<<taskid<<std::endl;
              sleep(1);
          };
      }
      void init(){
          fs.push_back(add);
          fs.push_back(divid);
          fs.push_back(mul);
      }
      int main(){
          init();
          srand((unsigned int)time(nullptr)^getpid());
          BlockQueue<Task> *bq=new BlockQueue<Task>();
          pthread_t c,p;
          pthread_create(&c,nullptr,comsumer,(void*)bq);
          pthread_create(&p,nullptr,productor,(void*)bq);
      
          pthread_join(c,nullptr);
          pthread_join(p,nullptr);
      }
      
      posted @ 2025-11-03 21:46  xdhking  閱讀(4)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 人妻18毛片A级毛片免费看| 无遮挡又黄又刺激的视频| 伊人精品成人久久综合97| 国内精品久久久久影视| 国语精品自产拍在线观看网站| 国产首页一区二区不卡| 东京热一精品无码av| 亚洲一区二区三区自拍偷拍| 国产精品十八禁一区二区| 精品国产自线午夜福利| 亚洲国产欧美一区二区好看电影 | 久9视频这里只有精品| 一本一本久久a久久精品综合| 柳州市| av综合亚洲一区二区| 日韩精品久久一区二区三| 国产精品视频全国免费观看| 亚洲精品人妻中文字幕| 久久se精品一区精品二区国产| 亚欧美日韩香蕉在线播放视频 | 亚洲精品二区在线播放| 被灌满精子的波多野结衣| 久久青青草原国产精品最新片| 国产一区二区精品久久呦| 男女激情一区二区三区| 久久综合色之久久综合色 | 国产福利精品一区二区| 亚洲av本道一区二区| 少妇xxxxx性开放| 亚洲乱亚洲乱妇50p| 免费观看全黄做爰大片| 国产精品国产亚洲看不卡| 久久SE精品一区精品二区| 定日县| 亚洲免费一区二区av| 欧美大bbbb流白水| 波多野结衣av高清一区二区三区| 麻豆久久久9性大片| 久久亚洲国产精品五月天| 欧美成人性色一区欧美成人性色区| 文昌市|