QT 中的生产者和消费者信号量
来源:互联网 发布:庄家统计软件破解 编辑:程序博客网 时间:2024/05/25 19:58
为了高效实现环形缓冲区的生产者和消费者的异步通信操作。我们需要用Qt中的QWaitCondition 或者QSemaphore
QWaitCondition介绍
QWaitCondition Class Reference
The QWaitCondition class provides a condition variable for synchronizing threads.
- #include <QWaitCondition>
Note: All functions in this class are thread-safe.
Detailed Description
The QWaitCondition class provides a condition variable for synchronizing threads.
QWaitCondition allows a thread to tell other threads that some sort of condition has been met. One or many threads can block waiting for a QWaitCondition to set a condition with wakeOne() or wakeAll(). Use wakeOne() to wake one randomly selected condition or wakeAll() to wake them all.
For example, let's suppose that we have three tasks that should be performed whenever the user presses a key. Each task could be split into a thread, each of which would have a run() body like this:
- forever {
- mutex.lock();
- keyPressed.wait(&mutex);
- do_something();
- mutex.unlock();
- }
Here, the keyPressed variable is a global variable of type QWaitCondition.
A fourth thread would read key presses and wake the other three threads up every time it receives one, like this:
- forever {
- getchar();
- keyPressed.wakeAll();
- }
The order in which the three threads are woken up is undefined. Also, if some of the threads are still in do_something() when the key is pressed, they won't be woken up (since they're not waiting on the condition variable) and so the task will not be performed for that key press. This issue can be solved using a counter and a QMutex to guard it. For example, here's the new code for the worker threads:
- forever {
- mutex.lock();
- keyPressed.wait(&mutex);
- ++count;
- mutex.unlock();
- do_something();
- mutex.lock();
- --count;
- mutex.unlock();
- }
Here's the code for the fourth thread:
- forever {
- getchar();
- mutex.lock();
- // Sleep until there are no busy worker threads
- while (count > 0) {
- mutex.unlock();
- sleep(1);
- mutex.lock();
- }
- keyPressed.wakeAll();
- mutex.unlock();
- }
The mutex is necessary because the results of two threads attempting to change the value of the same variable simultaneously are unpredictable.
Wait conditions are a powerful thread synchronization primitive. The Wait Conditions example shows how to use QWaitCondition as an alternative to QSemaphore for controlling access to a circular buffer shared by a producer thread and a consumer thread.
See also QMutex, QSemaphore, QThread, and Wait Conditions Example.
Public Functions
- Show all details for all members
QWaitCondition()
QWaitCondition~QWaitCondition()
boolQWaitConditionwait(QMutex *mutex , unsigned longtime=ULONG_MAX...)
boolQWaitConditionwait(QReadWriteLock *readWriteLock , unsigned longtime=ULONG_MAX...)
voidQWaitConditionwakeAll()
voidQWaitConditionwakeOne()
#include <QtCore> #include <stdio.h> #include <stdlib.h> const int DataSize = 100000; const int BufferSize = 8192; char buffer[BufferSize]; QWaitCondition bufferNotEmpty; QWaitCondition bufferNotFull; QMutex mutex; int numUsedBytes = 0; class Producer : public QThread { public: void run(); }; void Producer::run() { qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); for (int i = 0; i < DataSize; ++i) { mutex.lock(); if (numUsedBytes == BufferSize) bufferNotFull.wait(&mutex); mutex.unlock(); buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; mutex.lock(); ++numUsedBytes; bufferNotEmpty.wakeAll(); mutex.unlock(); } } class Consumer : public QThread { public: void run(); }; void Consumer::run() { for (int i = 0; i < DataSize; ++i) { mutex.lock(); if (numUsedBytes == 0) bufferNotEmpty.wait(&mutex); mutex.unlock(); fprintf(stderr, "%c", buffer[i % BufferSize]); mutex.lock(); --numUsedBytes; bufferNotFull.wakeAll(); mutex.unlock(); } fprintf(stderr, "\n"); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); Producer producer; Consumer consumer; producer.start(); consumer.start(); producer.wait(); consumer.wait(); return 0; }
QSemaphore介绍
QSemaphore Class Reference
The QSemaphore class provides a general counting semaphore.
- #include <QSemaphore>
Note: All functions in this class are thread-safe.
Detailed Description
The QSemaphore class provides a general counting semaphore.
A semaphore is a generalization of a mutex. While a mutex can only be locked once, it's possible to acquire a semaphore multiple times. Semaphores are typically used to protect a certain number of identical resources.
Semaphores support two fundamental operations, acquire() and release():
- acquire(n) tries to acquire n resources. If there aren't that many resources available, the call will block until this is the case.
- release(n) releases n resources.
There's also a tryAcquire() function that returns immediately if it cannot acquire the resources, and an available() function that returns the number of available resources at any time.
Example:
- QSemaphore sem(5); // sem.available() == 5
- sem.acquire(3); // sem.available() == 2
- sem.acquire(2); // sem.available() == 0
- sem.release(5); // sem.available() == 5
- sem.release(5); // sem.available() == 10
- sem.tryAcquire(1); // sem.available() == 9, returns true
- sem.tryAcquire(250); // sem.available() == 9, returns false
A typical application of semaphores is for controlling access to a circular buffer shared by a producer thread and a consumer thread. TheSemaphores example shows how to use QSemaphore to solve that problem.
A non-computing example of a semaphore would be dining at a restaurant. A semaphore is initialized with the number of chairs in the restaurant. As people arrive, they want a seat. As seats are filled, available() is decremented. As people leave, the available() is incremented, allowing more people to enter. If a party of 10 people want to be seated, but there are only 9 seats, those 10 people will wait, but a party of 4 people would be seated (taking the available seats to 5, making the party of 10 people wait longer).
See also QMutex, QWaitCondition, QThread, and Semaphores Example.
Public Functions
- Show all details for all members
QSemaphore(intn=0)
QSemaphore~QSemaphore()
voidQSemaphoreacquire(intn=1)
intQSemaphoreavailable()const
voidQSemaphorerelease(intn=1)
boolQSemaphoretryAcquire(intn=1)
boolQSemaphoretryAcquire(intn , inttimeout)
#include <QtCore> #include <stdio.h> #include <stdlib.h> const int DataSize = 100000; const int BufferSize = 8192; char buffer[BufferSize]; QSemaphore freeBytes(BufferSize); QSemaphore usedBytes; class Producer : public QThread { public: void run(); }; void Producer::run() { qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); for (int i = 0; i < DataSize; ++i) { freeBytes.acquire(); buffer[i % BufferSize] = "ACGT"[(int)qrand() % 4]; usedBytes.release(); } } class Consumer : public QThread { public: void run(); }; void Consumer::run() { for (int i = 0; i < DataSize; ++i) { usedBytes.acquire(); fprintf(stderr, "%c", buffer[i % BufferSize]); freeBytes.release(); } fprintf(stderr, "\n"); } int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); Producer producer; Consumer consumer; producer.start(); consumer.start(); producer.wait(); consumer.wait(); return 0; }
FROM: http://blog.163.com/qimo601@126/blog/static/1582209320121118112828662/
- QT 中的生产者和消费者信号量
- linux中的生产者和消费者问题--信号量 互斥 同步
- Qt生产者消费者实验(1):信号量
- Qt生产者消费者实验(1):信号量
- 信号量(生产者和消费者模型)
- 生产者-消费者信号量问题
- java中的生产者和消费者
- java中的信号量semaphore实现生产者消费者模式
- linux系统编程中的信号量--模拟生产者与消费者
- 用条件变量和信号量解决生产者和消费者问题
- iOS GCD 和信号量 实现 生产者和消费者模式
- -信号量(Semaphore)在生产者和消费者模式的使用
- 使用信号量和关键区来实现生产者消费者
- 通过生产者消费者问题比较信号量和信号【Java实现】
- 生产者消费者问题(信号量)
- 信号量实现生产者消费者问题
- 信号量解决生产者,消费者问题
- 信号量与生产者消费者问题
- 单节点配置SecondaryNameNode
- iOS工作中的问题-----navigationBar透明方法、遮挡UIViewController、UITableViewController 视图解决方法
- 用户画像数据建模方法
- CocoaPods详解之----使用篇
- 暴风一号1kb病毒又称快捷方式病毒
- QT 中的生产者和消费者信号量
- 典型崩溃问题集锦
- 基于文件过滤驱动的透明加解密
- DataTable简介
- 简单区分Vmware的三种网络连接模式(bridged、NAT、host-only)
- BZOJ 3993 [SDOI2015]星际战争 二分+最大流
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
- C#带ref和out的反射使用
- 云栖2015