国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

syncqueuq

2019-11-11 05:00:48
字體:
來源:轉載
供稿:網友

// block1

#PRagma once 

#include <list> #include <assert.h>  #include <Windows.h> 

class MutexLock{public: MutexLock() {  InitializeCriticalSection(&m_criticalSection); }

 ~MutexLock() {  DeleteCriticalSection(&m_criticalSection); }

 void lock() {  EnterCriticalSection(&m_criticalSection); }

 void unlock() {  LeaveCriticalSection(&m_criticalSection); }

 CRITICAL_SECTION& cs() {  return m_criticalSection; }

private: MutexLock(const MutexLock&); MutexLock& Operator=(const MutexLock&);

 CRITICAL_SECTION m_criticalSection;};

template<typename T>class BlockQueue{public: BlockQueue(int num = 256)  : m_num(num) {  init(); }

 ~BlockQueue() {  CloseHandle(m_semFull);  CloseHandle(m_semEmpty); }

 void put(const T &t) {  WaitForSingleObject(m_semFull, INFINITE);  m_mutexLock.lock();  m_queue.push_back(t);  m_mutexLock.unlock();  ReleaseSemaphore(m_semEmpty, 1, NULL); }

 void take(T &value) {  WaitForSingleObject(m_semEmpty, INFINITE);  m_mutexLock.lock();  assert(!m_queue.empty());  value = m_queue.front();  m_queue.pop_front();  m_mutexLock.unlock();  ReleaseSemaphore(m_semFull, 1, NULL); }

 size_t size() const  {   return m_queue.size();  }

private: void init() {  m_semFull = CreateSemaphore(NULL, m_num, m_num, NULL);  m_semEmpty = CreateSemaphore(NULL, 0, m_num, NULL);  assert(NULL != m_semFull);  assert(NULL != m_semEmpty); }

private: int m_num; MutexLock  m_mutexLock; HANDLE m_semFull; HANDLE m_semEmpty; std::list<T> m_queue;};

 

//block2

#pragma once

#include <condition_variable>#include <list>#include <assert.h>

template<typename T>class BlockQueue2{public: BlockQueue2() : m_mutex(), m_condvar(), m_queue() {

 }

 void put(const T& task) {  {   std::lock_guard<std::mutex> lock(m_mutex);   m_queue.push_back(task);  }  m_condvar.notify_all(); }

 void take(T &value) {  std::unique_lock<std::mutex> lock(m_mutex);  m_condvar.wait(lock, [this]{return !m_queue.empty(); });  assert(!m_queue.empty());  value = m_queue.front();  m_queue.pop_front(); }

 size_t size() const {  std::lock_guard<std::mutex> lock(m_mutex);  return m_queue.size(); }

private: BlockQueue2(const BlockQueue2& rhs); BlockQueue2& operator = (const BlockQueue2& rhs);

private: mutable std::mutex m_mutex; std::condition_variable m_condvar; std::list<T> m_queue;};

 

//main

//test.cpp

#include "SyncQueue.h"#include "SyncQueue2.h"#include <thread>#include <iostream>#include <chrono>#include <time.h>#include <stdlib.h>

BlockQueue<int> syncQueue;//BlockQueue2<int> syncQueue;void Produce(int producerID){ /*while (true) {  //std::this_thread::sleep_for(std::chrono::seconds(1));  srand((unsigned)time(NULL));  int x = rand() % 1000;  syncQueue.Put(x);  std::cout << "consumer[" << consumerID << "] take:" << x << std::endl; }*/

 for (int i = 0; i < 100; ++i) {  std::this_thread::sleep_for(std::chrono::seconds(1));  syncQueue.put(i);  //std::cout << "put:" << i << std::endl;  std::cout << "producer[" << producerID << "] put:" << i << std::endl; }}

void Consume(int consumerID){ int x = 0;

 while (true) {  //std::this_thread::sleep_for(std::chrono::seconds(1));  syncQueue.take(x);  std::cout << "consumer[" << consumerID << "] take:" << x << std::endl; }}

int main(void){ std::thread producer1(Produce, 1); std::thread producer2(Produce, 2); std::thread consumer1(Consume, 1); std::thread consumer2(Consume, 2); std::thread consumer3(Consume, 3); producer1.join(); producer2.join(); consumer1.join(); consumer2.join(); consumer3.join();

 return 0;}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 襄城县| 南郑县| 鄂伦春自治旗| 化德县| 南乐县| 南溪县| 卢氏县| 双牌县| 宜宾市| 平利县| 吉林省| 浦城县| 凌源市| 会宁县| 云安县| 黄骅市| 嘉义市| 海口市| 高雄市| 上饶县| 昔阳县| 京山县| 安化县| 哈密市| 南投县| 张家港市| 海盐县| 大关县| 高唐县| 托里县| 运城市| 合肥市| 永丰县| 荆州市| 荃湾区| 乐安县| 子洲县| 黑河市| 名山县| 乌拉特中旗| 垦利县|