博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows下一个并发阻塞队列(BlockingQueue)
阅读量:7026 次
发布时间:2019-06-28

本文共 2282 字,大约阅读时间需要 7 分钟。

Windows下一个带有大小限制的并发阻塞队列,实现的比较简单。

#ifndef BLOCKINGQUEUE_H_#define BLOCKINGQUEUE_H_#include 
#include
using namespace std;template
class BoundedBlockingQueue { public: BoundedBlockingQueue(int size) : maxSize(size) { _lock = CreateMutex(NULL,false,NULL); _rsem = CreateSemaphore(NULL,0,size,NULL); _wsem = CreateSemaphore(NULL,size,size,NULL); } ~BoundedBlockingQueue() { CloseHandle(_lock); CloseHandle(_rsem); CloseHandle(_wsem); } void push(const T& data); T pop(); bool empty() { WaitForSingleObject(_lock,INFINITE); bool is_empty = _array.empty(); ReleaseMutex(_lock); return is_empty; }private: deque
_array; int maxSize; HANDLE _lock; HANDLE _rsem, _wsem;};template
void BoundedBlockingQueue
::push(const T& value ) { WaitForSingleObject(_wsem,INFINITE); WaitForSingleObject(_lock,INFINITE); _array.push_back(value); ReleaseMutex(_lock); ReleaseSemaphore(_rsem,1,NULL);}template
T BoundedBlockingQueue
::pop() { WaitForSingleObject(_rsem,INFINITE); WaitForSingleObject(_lock,INFINITE); T _temp = _array.front(); _array.pop_front(); ReleaseMutex(_lock); ReleaseSemaphore(_wsem,1,NULL); return _temp;}#endif

主函数调用测试:一个生产者、两个消费者使用这个队列进行测试。

#include "BlockingQueue.h"#include 
#include
using namespace std;bool is_over = false;DWORD WINAPI produce(LPVOID lppara){ BoundedBlockingQueue
*queue = (BoundedBlockingQueue
*)lppara; while(1) { for(int i=1; i<=50; ++i) { queue->push(i); cout<
<<" put a data: "<
<
*queue = (BoundedBlockingQueue
*)lppara; while(1) { int d = queue->pop(); cout<
<<" get data: "<
<
empty()) { cout<<"OVER!"<
queue(20); //一个生产者、两个消费者 if(CreateThread(NULL,0,produce,&queue,0,&write_data)==NULL) return -1; if(CreateThread(NULL,0,consume,&queue,0,&read_data)==NULL) return -1; if(CreateThread(NULL,0,consume,&queue,0,&read_data1)==NULL) return -1; char ch; while(1) { ch = getchar(); //press "e" to exit if(ch == 'e') break; } printf("Program ends successfully\n"); return 0;}

转载地址:http://nrlxl.baihongyu.com/

你可能感兴趣的文章