Linux C++ 使用condition實現(xiàn)阻塞隊列的方法
更新時間:2017年01月06日 10:04:49 投稿:jingxian
下面小編就為大家?guī)硪黄狶inux C++ 使用condition實現(xiàn)阻塞隊列的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
實例如下:
/*
* 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;
}
以上就是小編為大家?guī)淼腖inux C++ 使用condition實現(xiàn)阻塞隊列的方法全部內(nèi)容了,希望大家多多支持腳本之家~
您可能感興趣的文章:
相關(guān)文章
詳解CentOS7 FTP服務(wù)搭建(虛擬用戶訪問FTP服務(wù))
Liunx外部文件的傳輸,避免不了使用FTP服務(wù),所以現(xiàn)在就整理下,CentOS7環(huán)境下,F(xiàn)TP服務(wù)的搭建。有興趣的可以了解一下。2017-01-01
判斷Unix系統(tǒng)及庫文件是32位還是64位的詳解
這篇文章主要介紹了判斷Unix系統(tǒng)及庫文件是32位還是64位的的相關(guān)資料,這里整理下查看系統(tǒng)位數(shù)的命令,需要的朋友可以參考下2016-11-11
SpringBoot整合Activiti7的實現(xiàn)代碼
這篇文章主要介紹了SpringBoot整合Activiti7的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
apache You don''t have permission to access /test.php on thi
這篇文章主要介紹了apache You don't have permission to access /test.php on this server解決方法,需要的朋友可以參考下2015-04-04
關(guān)于Windows 不能在 本地計算器 啟動 Apache2(phpstudy)
今天在自己的本子上準(zhǔn)備放多個虛擬站點。用的是#phpstudy#。在軟件自身的站點設(shè)置中,根據(jù)提示添加的多站點無效不知道是否和我的系統(tǒng)是Win7有關(guān)2012-09-09
使用ssh-keygen,實現(xiàn)免密碼登陸linux的方法
下面小編就為大家?guī)硪黄褂胹sh-keygen,實現(xiàn)免密碼登陸linux的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
Linux:FTP工具及SSH遠(yuǎn)程連接工具的使用方式
這篇文章主要介紹了Linux:FTP工具及SSH遠(yuǎn)程連接工具的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02

