Linux C++ 使用condition實(shí)現(xiàn)阻塞隊(duì)列的方法
實(shí)例如下:
/* * 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實(shí)現(xiàn)阻塞隊(duì)列的方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
Linux 內(nèi)核空間與用戶空間實(shí)現(xiàn)與分析
這篇文章主要介紹了Linux 內(nèi)核空間與用戶空間實(shí)現(xiàn)與分析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02Linux配置防火墻,開啟80、3306端口的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于Linux配置防火墻,開啟80端口、3306端口的相關(guān)內(nèi)容,需要的朋友們參考下。2020-02-02linux服務(wù)器ubuntu定時(shí)任務(wù)cron設(shè)置每秒執(zhí)行
這篇文章主要介紹了linux服務(wù)器ubuntu定時(shí)任務(wù)cron設(shè)置每秒執(zhí)行,使用 cron 時(shí),有一些注意事項(xiàng)可以幫助你確保任務(wù)按預(yù)期執(zhí)行,并減少潛在的問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-02-02詳解Centos7中Nginx開機(jī)自啟動(dòng)的解決辦法
本篇文章主要介紹了詳解Centos7中Nginx開機(jī)自啟動(dòng)的解決辦法,具有一定的參加價(jià)值,有興趣的可以了解一下。2017-03-03Linux中特殊權(quán)限SUID、SGID與SBIT的深入講解
linux對文件的權(quán)限管理簡直是讓人嘆為觀止,所以這篇文章主要給大家介紹了關(guān)于Linux中特殊權(quán)限SUID、SGID與SBIT的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫的簡單方法
在本篇文章中我們給大家整理了一些關(guān)于linux實(shí)現(xiàn)定時(shí)備份mysql數(shù)據(jù)庫的簡單方法,有需要的朋友們可以學(xué)習(xí)下。2018-09-09Linux環(huán)境使用g++編譯C++方法總結(jié)
本篇文章給大家分享了在Linux環(huán)境中用g++編譯C++的方法以及相關(guān)實(shí)例代碼分享,有興趣的朋友學(xué)習(xí)下。2018-03-03