zoukankan      html  css  js  c++  java
  • 生产者,消费者 多线程

      

    #include <iostream>
    #include <vector>
    #include <thread>
    #include <mutex>
    
    const size_t SIZE = 10;
    using ElemTy = char;
    ElemTy arr[SIZE];
    
    size_t begin = 0;
    size_t end = 0;
    
    std::mutex prdc_m;
    std::mutex cnsm_m;
    
    void produce() {
      for (;;) {
        if ((end + 1) % SIZE != begin) {
          {
            std::lock_guard<std::mutex> lg(prdc_m);
            arr[end] = 'o';
            end = (end + 1) % SIZE;
            std::cout << "produce and size is " << (end - begin) % SIZE << std::endl;
          }
        }
      }
    }
    
    void consume() {
      for (;;) {
        if (begin != end) {
          ElemTy e;
          {
            std::lock_guard<std::mutex> lg(cnsm_m);
            e = arr[begin];
            begin = (begin + 1) % SIZE;
            std::cout << "consume and size is " << (end - begin) % SIZE << std::endl;
          }
        }
      }
    }
    
    int main() {
      std::vector<std::thread> vc;
      std::vector<std::thread> vp;
      for (int i = 0; i < 10; ++i) {
        vc.push_back(std::thread(consume));
      }
      for (int i = 0; i < 10; ++i) {
        vp.push_back(std::thread(produce));
      }
    
      for (auto &t : vc) {
        t.join();
      }
    
      for (auto &t : vp) {
        t.join();
      }
    
      return 0;
    }
    #include <iostream>
    #include <vector>
    #include <thread>
    #include <mutex>
    
    class ConcurrentQueue {
      using ElemTy = char;
      static const size_t SIZE = 10;
      ElemTy arr[SIZE];
    
      size_t begin = 0;
      size_t end = 0;
    
      std::mutex prdc_m;
      std::mutex cnsm_m;
    
    public:
      void produce() {
        for (;;) {
          if ((end + 1) % SIZE != begin) {
            {
              std::lock_guard<std::mutex> lg(prdc_m);
              arr[end] = 'o';
              end = (end + 1) % SIZE;
              std::cout << "produce and size is " << (end - begin) % SIZE << std::endl;
            }
          }
        }
      }
    
      void consume() {
        for (;;) {
          if (begin != end) {
            ElemTy e;
            {
              std::lock_guard<std::mutex> lg(cnsm_m);
              e = arr[begin];
              begin = (begin + 1) % SIZE;
              std::cout << "consume and size is " << (end - begin) % SIZE << std::endl;
            }
          }
        }
      }
    };
    
    int main() {
      std::vector<std::thread> vc;
      std::vector<std::thread> vp;
      ConcurrentQueue Q;
      for (int i = 0; i < 10; ++i) {
        vc.push_back(std::thread(&ConcurrentQueue::consume, std::ref(Q)));
      }
      for (int i = 0; i < 10; ++i) {
        vp.push_back(std::thread(&ConcurrentQueue::produce, std::ref(Q)));
      }
    
      for (auto &t : vc) {
        t.join();
      }
    
      for (auto &t : vp) {
        t.join();
      }
    
      return 0;
    }
    

      

  • 相关阅读:
    无重复字符的最长子串
    最长公共前缀
    项目开发的 工程化
    包管理 import debug 模块管理 module
    Third Party Browser Drivers NOT DEVELOPED by seleniumhq
    任何不看源码的代码引入都是存在定时爆炸的可能
    博客数计数
    lineage 世系 血缘 容错机制 DAG
    查源码分析 游标 写 需要 cursors 一切不看源码的代码引入都是定时炸弹的启动
    8核 16g 及时释放内存空间
  • 原文地址:https://www.cnblogs.com/jjtx/p/6759605.html
Copyright © 2011-2022 走看看