C++实现生产者消费者队列
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C++实现生产者消费者队列
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                C++實(shí)現(xiàn)生產(chǎn)者消費(fèi)者隊(duì)列
- 分析
 - 程序
 - 隊(duì)列的類
 - 生產(chǎn)者邏輯
 - 消費(fèi)者邏輯
 - 主函數(shù)
 
- 結(jié)果分析
 - 源碼地址
 
分析
首先,我們的生產(chǎn)者與消費(fèi)者隊(duì)列需要滿足同步與互斥關(guān)系,就需要一把互斥鎖,以及生產(chǎn)者與消費(fèi)者各自的條件變量。
 其次,我們可以利用C++中STL里的queue隊(duì)列來進(jìn)行實(shí)現(xiàn),但是我們需要對(duì)push,pop進(jìn)行修改,因?yàn)镾TL庫(kù)的函數(shù)不一定能滿足互斥條件。也就是不一定安全。
 最后,所有資源在程序結(jié)束后一定要記得釋放,否則會(huì)出現(xiàn)內(nèi)存泄漏的風(fēng)險(xiǎn)。
程序
隊(duì)列的類
/thread safe queue class BlockQueue { public:BlockQueue(size_t Capacity = CAPACITY){_Capacity = Capacity;pthread_mutex_init(&_Lock, NULL);pthread_cond_init(&_ConsumeCond, NULL);pthread_cond_init(&_ProductCond, NULL);}//push == Producer void Push(int& Data){pthread_mutex_lock(&_Lock);//判斷隊(duì)列有沒有滿 while(IsFull()){pthread_cond_wait(&_ProductCond,&_Lock);}_Queue.push(Data);pthread_mutex_unlock(&_Lock);pthread_cond_signal(&_ConsumeCond);}//pop == consumervoid Pop(int* Data){pthread_mutex_lock(&_Lock);//判斷隊(duì)列有沒有空 while(_Queue.empty()){pthread_cond_wait(&_ConsumeCond, &_Lock); } *Data = _Queue.front();_Queue.pop();pthread_mutex_unlock(&_Lock);pthread_cond_signal(&_ProductCond);}~BlockQueue(){pthread_mutex_destroy(&_Lock);pthread_cond_destroy(&_ConsumeCond);pthread_cond_destroy(&_ProductCond);} private:bool IsFull(){if(_Queue.size() == _Capacity)return true;return false;} private:queue<int> _Queue;size_t _Capacity; // queue max capacity //互斥pthread_mutex_t _Lock;//mutex//同步 pthread_cond_t _ConsumeCond; // consume cond pthread_cond_t _ProductCond; // product _ProductCond };生產(chǎn)者邏輯
void* Producter_start(void* arg) {BlockQueue* que = (BlockQueue*)arg;int resource = 1;while(1){que->Push(resource);resource++;printf("\n\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");printf("Producter_thread : [%p]\n",pthread_self());printf("i product resource [%d]\n",resource - 1);printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");}return NULL;}消費(fèi)者邏輯
void* Consumer_start(void* arg) {BlockQueue* que = (BlockQueue*)arg;while(1){int Data;que->Pop(&Data);printf("\n\n######################################\n");printf("Consumer_thread : [%p]\n", pthread_self());printf("i consume resource [%d]\n", Data);printf("######################################\n");}return NULL; }主函數(shù)
int main() {BlockQueue* que = new BlockQueue();pthread_t com_tid[THREADCOUNT], pro_tid[THREADCOUNT];int ret = 0;for(int i = 0; i < THREADCOUNT; i++){ret = pthread_create(&com_tid[i],NULL,Consumer_start,(void*)que);if(ret < 0){perror("pthread_create Consumer error");return 0;}ret = pthread_create(&pro_tid[i],NULL,Producter_start,(void*)que);if(ret < 0){perror("pthread_create Producter error");}}//thread wait for(int i = 0; i < THREADCOUNT; i++){pthread_join(com_tid[i],NULL);pthread_join(pro_tid[i],NULL);}//防止內(nèi)存泄漏 delete que;que = NULL;return 0; }結(jié)果分析
 我們發(fā)現(xiàn)最后的結(jié)果中,有一部分生產(chǎn)者與消費(fèi)者信息的打印好像不是那么規(guī)范,但實(shí)際上他們都是合理的訪問臨界資源的。
因?yàn)榫€程之間的搶占式執(zhí)行,使得每一個(gè)線程只能擁有一個(gè)CPU資源一小會(huì),然后就需要讓出CPU資源給其他線程。然后就會(huì)出現(xiàn)上圖所示的執(zhí)行邏輯。
源碼地址
https://github.com/duchenlong/linux-text/blob/master/thread/threadqueue.cpp
總結(jié)
以上是生活随笔為你收集整理的C++实现生产者消费者队列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 半导体器件物理【5】固体量子 —— 能带
 - 下一篇: 如何查询网站IP地址