麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 服務器 > Linux服務器 > 正文

Linux C++ 使用condition實現阻塞隊列的方法

2024-09-05 23:03:27
字體:
來源:轉載
供稿:網友

實例如下:

/* * BlockingQueue.h * * Created on: 2014年6月10日 *   Author:  */#ifndef BLOCKINGQUEUE_H_#define BLOCKINGQUEUE_H_#include <iostream>#include <pthread.h>using namespace std;//template <typename T >class BlockingQueue{public:	BlockingQueue();	BlockingQueue(int capacity);	~BlockingQueue();	bool push(int item);	int poll();private:	int capacity;	int* queue;	int head,tail;	pthread_mutex_t mutex;	pthread_cond_t notFull,notEmpty;};#endif /* BLOCKINGQUEUE_H_ */
/* * BlockingQueue.cpp * *  Created on: 2014年6月10日 *      Author:  */#include "../include/BlockingQueue.h"BlockingQueue::BlockingQueue(){    this->capacity = 10;    queue = new int[capacity];    head = 0,tail = 0;    pthread_mutex_init(&mutex,NULL);    pthread_cond_init(¬Full,NULL);    pthread_cond_init(¬Empty,NULL);}BlockingQueue::BlockingQueue(int capacity){    this->capacity = capacity;    queue = new int[capacity];    cout << "capacity " << sizeof(queue) << endl;    head = 0,tail = 0;    pthread_mutex_init(&mutex,NULL);    pthread_cond_init(¬Full,NULL);    pthread_cond_init(¬Empty,NULL);}BlockingQueue::~BlockingQueue(){    this->capacity = 0;    head = 0,tail = 0;    delete queue;    pthread_mutex_destroy(&mutex);    pthread_cond_destroy(¬Full);    pthread_cond_destroy(¬Empty);}bool BlockingQueue::push(int item){    pthread_mutex_lock(&mutex);    cout << "you want push " << item << endl;    while((head + 1) % capacity == tail)//is full    {        cout << "is full,wait..." << endl;        // push wait        pthread_cond_wait(¬Full,&mutex);        cout << "not full,unlock" << endl;    }    {        queue[head] = item;        head = (head + 1) % capacity;        cout << "push " << item << endl;        //wake up poll thread        pthread_cond_signal(¬Empty);        pthread_mutex_unlock(&mutex);        return true;    }}int BlockingQueue::poll(){    pthread_mutex_lock(&mutex);    int ret = 0;    while(head == tail) // is empty    {        cout << "is empty,wait..." << endl;        //poll wait        pthread_cond_wait(¬Empty,&mutex);        cout << "not empty,unlock..." << endl;    }    {        ret = queue[tail];        tail = (tail + 1) % capacity;        cout << "take " << ret << endl;        //wake up push thread        pthread_cond_signal(¬Full);        pthread_mutex_unlock(&mutex);        return ret;    }}
#include <iostream>#include "include/BlockingQueue.h"using namespace std;BlockingQueue queue(3);void* put(void *){	queue.push(1);	  queue.push(2);	  queue.push(3);	  queue.push(4);	  queue.push(5);	  return NULL;}void* take(void *){	queue.poll();	queue.poll();	queue.poll();	return NULL;}int main() {	pthread_t put1,take1;  pthread_create(&put1,NULL,put,0);  pthread_create(&take1,NULL,take,0);  void * retval;  pthread_join(put1,&retval);  pthread_join(take1,&retval);	return 0;}

以上就是小編為大家帶來的Linux C++ 使用condition實現阻塞隊列的方法全部內容了,希望大家多多支持VEVB武林網~


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲一区二区三区在线免费观看 | 亚洲免费观看视频 | 毛片国产| 免费看欧美黑人毛片 | 国产精品视频在 | 91亚洲免费视频 | 免费久久久 | 欧美高清第一页 | 成人在线精品视频 | 亚洲国产视频在线 | 中文字幕国产欧美 | 国产在线一级片 | 男女一边摸一边做羞羞视频免费 | 欧美三区在线 | 最新se94se在线欧美 | 日韩精品中文字幕在线播放 | 免费久久久| 亚洲一区在线国产 | 久久探花 | 看免费黄色大片 | 欧美一级毛片大片免费播放 | 国产91精品久久久久久久 | 久久老司机精品视频 | 4p一女两男做爰在线观看 | 999插插插| 免费看成人av | 强伦女教师视频 | 热久久91 | 久久久久久久九九九九 | 在线看小早川怜子av | 男女羞羞视频在线免费观看 | 91久久久久久 | 欧美国产一区二区三区激情无套 | 久久久www成人免费毛片 | 成人av一二三区 | 欧美3p激情一区二区三区猛视频 | 精品三区视频 | 极品美女一级毛片 | 在线 日本 制服 中文 欧美 | 一级黄色a视频 | 可以免费看av |