Qt生產者消費者實驗(2):等待條件QWaitCondition
轉自:http://blog.csdn.net/xufenghfut/article/details/7857364
- /*
- 使用QWaitCondition和QMutex可以實現比訊號量更精確的控制
- */
- #include <QtCore>
- #include <iostream>
- //const int DataSize = 100000;
- //const int BufferSize = 4096;
- const int DataSize = 100;
- const int BufferSize = 100;
- char buffer[BufferSize];
- //與訊號量不同的程式
- QWaitCondition bufferIsNotFull; //QWaitCondition允許在一定條件下觸發其它多個執行緒
- QWaitCondition bufferIsNotEmpty;
- QMutex mutex;
- int usedSpace = 0;//不是訊號量,而是用來代表在緩衝器中存在多少個“用過的”位元組
- //生產者
- class Producer : public QThread
- {
- public:
- void run();
- };
- void Producer::run()
- {
- for (int i = 0; i < DataSize; ++i) {
- mutex.lock();//鎖定:保護對usedSpace變數的訪問
- //如果緩衝區都是用過的位元組,那麼就等待
- while (usedSpace == BufferSize)
- bufferIsNotFull.wait(&mutex);//wait的過程:解鎖->阻塞當前執行緒->滿足條件->鎖定->返回
- buffer[i % BufferSize] = "ACGT"[uint(rand()) % 4];
- std::cerr<<"P";//代表一次生產者執行緒的執行
- ++usedSpace;
- bufferIsNotEmpty.wakeAll();//Wakes all threads waiting on the wait condition.
- mutex.unlock();//解鎖
- }
- }
- //消費者
- class Consumer : public QThread
- {
- public:
- void run();
- };
- void Consumer::run()
- {
- for (int i = 0; i < DataSize; ++i) {
- mutex.lock();//
- //如果緩衝區裡“用過的”位元組數目為0,則等待
- while (usedSpace == 0)
- bufferIsNotEmpty.wait(&mutex);
- //std::cerr << buffer[i % BufferSize];
- char Cons = buffer[i % BufferSize];
- std::cerr<<"c";
- --usedSpace;
- bufferIsNotFull.wakeAll();
- mutex.unlock();
- }
- std::cerr << std::endl;
- }
- int main()
- {
- Producer producer;
- Consumer consumer;
- producer.start();
- consumer.start();
- producer.wait();
- consumer.wait();
- return 0;
- }
(1)
- const int DataSize = 100;
- const int BufferSize = 1;
(2)
- const int DataSize = 100;
- const int BufferSize = 10;
PPPPPPPPPPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcccccccccc
結果2
PPPPPPPPPPcPcPcPcPcPcPcPcPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPcc
結果3
PPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPcccccccccc
(3)
- const int DataSize = 100;
- const int BufferSize = 100;
- /*
- 使用QWaitCondition和QMutex可以實現比訊號量更精確的控制
- */
- #include <QtCore>
- #include <iostream>
- //const int DataSize = 100000;
- //const int BufferSize = 4096;
- const int DataSize = 100;
- const int BufferSize = 100;
- char buffer[BufferSize];
- //與訊號量不同的程式
- QWaitCondition bufferIsNotFull; //QWaitCondition允許在一定條件下觸發其它多個執行緒
- QWaitCondition bufferIsNotEmpty;
- QMutex mutex;
- int usedSpace = 0;//不是訊號量,而是用來代表在緩衝器中存在多少個“用過的”位元組
- //生產者
- class Producer : public QThread
- {
- public:
- void run();
- };
- void Producer::run()
- {
- for (int i = 0; i < DataSize; ++i) {
- mutex.lock();//鎖定:保護對usedSpace變數的訪問
- //如果緩衝區都是用過的位元組,那麼就等待
- while (usedSpace == BufferSize)
- bufferIsNotFull.wait(&mutex);//wait的過程:解鎖->阻塞當前執行緒->滿足條件->鎖定->返回
- buffer[i % BufferSize] = "ACGT"[uint(rand()) % 4];
- std::cerr<<"P";//代表一次生產者執行緒的執行
- ++usedSpace;
- bufferIsNotEmpty.wakeAll();//Wakes all threads waiting on the wait condition.
- mutex.unlock();//解鎖
- }
- }
- //消費者
- class Consumer : public QThread
- {
- public:
- void run();
- };
- void Consumer::run()
- {
- for (int i = 0; i < DataSize; ++i) {
- mutex.lock();//
- //如果緩衝區裡“用過的”位元組數目為0,則等待
- while (usedSpace == 0)
- bufferIsNotEmpty.wait(&mutex);
- //std::cerr << buffer[i % BufferSize];
- char Cons = buffer[i % BufferSize];
- std::cerr<<"c";
- --usedSpace;
- bufferIsNotFull.wakeAll();
- mutex.unlock();
- }
- std::cerr << std::endl;
- }
- int main()
- {
- Producer producer;
- Consumer consumer;
- producer.start();
- consumer.start();
- producer.wait();
- consumer.wait();
- return 0;
- }
(1)
- const int DataSize = 100;
- const int BufferSize = 1;
(2)
- const int DataSize = 100;
- const int BufferSize = 10;
PPPPPPPPPPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcPcccccccccc
結果2
PPPPPPPPPPcPcPcPcPcPcPcPcPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPcc
結果3
PPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPccccccccccPPPPPPPPPPcccccccccc
(3)
- const int DataSize = 100;
- const int BufferSize = 100;
相關文章
- Qt基於QSemaphore的生產者消費者模型QT模型
- 生產者消費者模式模式
- 生產者消費者模型模型
- Java實現生產者和消費者Java
- Java實現生產者-消費者模型Java模型
- 生產者和消費者(.net實現)
- 生產消費者模式模式
- java實現生產者消費者問題Java
- 九、生產者與消費者模式模式
- python 生產者消費者模式Python模式
- ActiveMQ 生產者和消費者demoMQ
- 多執行緒-生產者消費者之等待喚醒機制執行緒
- 使用BlockQueue實現生產者和消費者模式BloC模式
- 使用Disruptor實現生產者和消費者模型模型
- PHP操作Beanstalkd佇列(2)生產者與消費者PHPBean佇列
- Kafka 簡單實驗二(Python實現簡單生產者消費者)KafkaPython
- 新手練習-消費者生產者模型模型
- 「Kafka應用」PHP實現生產者與消費者KafkaPHP
- 使用slice和條件變數實現一個簡單的多生產者多消費者佇列變數佇列
- 二、(LINUX 執行緒同步) 互斥量、條件變數以及生產者消費者問題Linux執行緒變數
- 阻塞佇列和生產者-消費者模式佇列模式
- linux 生產者與消費者問題Linux
- 多執行緒之生產者消費者執行緒
- 直觀理解生產者消費者問題
- Java 生產者消費者模式詳細分析Java模式
- 分享一個生產者-消費者的真實場景
- 有名訊號量實現消費者生產者問題
- 生產者消費者問題-C++程式碼實現C++
- 讀者寫者與生產者消費者應用場景
- C++ condition_variable 實現生產者消費者模型C++模型
- 使用wait()與notifyAll()實現生產者與消費者模式AI模式
- rabbitMQ實戰生產者-交換機-佇列-消費者細談MQ佇列
- Java多執行緒——生產者消費者示例Java執行緒
- 生產者與消費者之Android audioAndroid
- java編寫生產者/消費者模式的程式。Java模式
- 併發設計模式---生產者/消費者模式設計模式
- Python中的生產者消費者問題Python
- JAVA執行緒消費者與生產者模型Java執行緒模型