日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现生产者消费者队列

發(fā)布時間:2024/3/12 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现生产者消费者队列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C++實現(xiàn)生產(chǎn)者消費者隊列

  • 分析
  • 程序
    • 隊列的類
    • 生產(chǎn)者邏輯
    • 消費者邏輯
    • 主函數(shù)
  • 結(jié)果分析
  • 源碼地址

分析

首先,我們的生產(chǎn)者與消費者隊列需要滿足同步與互斥關系,就需要一把互斥鎖,以及生產(chǎn)者與消費者各自的條件變量。
其次,我們可以利用C++中STL里的queue隊列來進行實現(xiàn),但是我們需要對push,pop進行修改,因為STL庫的函數(shù)不一定能滿足互斥條件。也就是不一定安全。
最后,所有資源在程序結(jié)束后一定要記得釋放,否則會出現(xiàn)內(nèi)存泄漏的風險。

程序

隊列的類

/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);//判斷隊列有沒有滿 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);//判斷隊列有沒有空 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;}

消費者邏輯

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)者與消費者信息的打印好像不是那么規(guī)范,但實際上他們都是合理的訪問臨界資源的。

因為線程之間的搶占式執(zhí)行,使得每一個線程只能擁有一個CPU資源一小會,然后就需要讓出CPU資源給其他線程。然后就會出現(xiàn)上圖所示的執(zhí)行邏輯。

源碼地址

https://github.com/duchenlong/linux-text/blob/master/thread/threadqueue.cpp

總結(jié)

以上是生活随笔為你收集整理的C++实现生产者消费者队列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。