生产者跟消费者问题(C++实现)
生活随笔
收集整理的這篇文章主要介紹了
生产者跟消费者问题(C++实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼簡述
- T 表示有T個生產者。同時也有T個消費者。
然后每個生產者總共生產T個數據。
每個消費者總共消費T個數據。 - N表示最大隨機數大數值
- 那個信號量的實現,我是直接copy課件上的。
代碼
版本一
#include <iostream> #include <string> #include <thread> #include <mutex> #include <queue> #include <vector> #include <ctime> #include <random> using namespace std;class Semaphore { public:Semaphore(long count = 0) :count_(count) {}void Signal() {unique_lock<mutex> lock(mutex_);++count_;cv_.notify_one();}void Wait() {unique_lock<mutex> lock(mutex_);//第二個參數為false時才會阻塞,//為使用Lambda表達式創建匿名函數cv_.wait(lock, [=] { return count_ > 0; });--count_;}void setCount(long count = 0) {count_ = count;} private:mutex mutex_;condition_variable cv_;long count_; };int T, N; int BufferSize; queue<string> output; queue<int> Buffer; Semaphore BufferChoose, BufferCnt, BufferLast;void Poducer(int cnt) {srand(time(NULL));int data;for (int i = 0; i < T; ++i) {BufferLast.Wait();BufferChoose.Wait();data = rand() % N;Buffer.push(data);string temp = string("生產者") + to_string(cnt) + string("向緩沖區輸出了") + to_string(data);output.push(temp);BufferChoose.Signal();BufferCnt.Signal();} }void Consumer(int cnt) {int data;for (int i = 0; i < T; ++i) {BufferCnt.Wait();BufferChoose.Wait();data = Buffer.front();Buffer.pop();string temp = string("消費者") + to_string(cnt) + string("從緩沖區取到了") + to_string(data) + string(",平方得") + to_string(data * data);output.push(temp);BufferChoose.Signal();BufferLast.Signal();} }int main() {T = 4;N = 100;BufferSize = 2;_ASSERT(BufferSize > 0);BufferChoose.setCount(1);BufferCnt.setCount(0);BufferLast.setCount(BufferSize);vector<thread> vt;for (int i = 0; i < 2*T; ++i) {if (i < T) vt.push_back(thread(Poducer, i));else vt.push_back(thread(Consumer, i-T));}for (int i = 0; i < 2*T; ++i) {vt[i].join();}while (!output.empty()) {cout << output.front() << endl;output.pop();}system("pause"); }版本二
#include <iostream> #include <string> #include <thread> #include <mutex> #include <queue> #include <vector> #include <ctime> #include <random> using namespace std;class Semaphore { public:Semaphore(long count = 0) :count_(count) {}void Signal() {unique_lock<mutex> lock(mutex_);++count_;cv_.notify_one();}void Wait() {unique_lock<mutex> lock(mutex_);//第二個參數為false時才會阻塞,//為使用Lambda表達式創建匿名函數cv_.wait(lock, [=] { return count_ > 0; });--count_;}void setCount(long count = 0) {count_ = count;} private:mutex mutex_;condition_variable cv_;long count_; };int T, N; int BufferSize; queue<string> output; queue<int> Buffer; Semaphore BufferChoose, BufferCnt, BufferLast;void Poducer(int cnt) {srand(time(NULL)+cnt);int data;for (int i = 0; i < T; ++i) {BufferLast.Wait();BufferChoose.Wait();data = rand() % N;Buffer.push(data);string temp = string("生產者") + to_string(cnt) + string("向緩沖區輸出了") + to_string(data);output.push(temp);BufferChoose.Signal();BufferCnt.Signal();} }void Consumer(int cnt) {int data;for (int i = 0; i < T; ++i) {BufferCnt.Wait();BufferChoose.Wait();data = Buffer.front();Buffer.pop();string temp = string("消費者") + to_string(cnt) + string("從緩沖區取到了") + to_string(data) + string(",平方得") + to_string(data * data);output.push(temp);BufferChoose.Signal();BufferLast.Signal();} }int main() {T = 4;N = 100;BufferSize = 10;_ASSERT(BufferSize > 0);BufferChoose.setCount(1);BufferCnt.setCount(0);BufferLast.setCount(BufferSize);vector<thread> vt;for (int i = 0; i < 2*T; ++i) {if (i < T) vt.push_back(thread(Poducer, i));else vt.push_back(thread(Consumer, i-T));}for (int i = 0; i < 2*T; ++i) {vt[i].join();}while (!output.empty()) {cout << output.front() << endl;output.pop();}system("pause"); } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的生产者跟消费者问题(C++实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多协程爬取中大微博内容(以及转发数,点赞
- 下一篇: 斯特芬森迭代法(Python)