演示使用 Qt 进行多线程编程。
The Wait Conditions example shows how to use
QWaitConditionandQMutexto control access to a circular buffer shared by a producer thread and a consumer thread.生产者把数据写入缓冲直到达到缓冲末端为止,此时它从头重新开始,覆盖现有数据。消费者线程读取产生数据并将其写入标准错误。
相比单独采用互斥,等待条件使之拥有更高级的并发成为可能。若对缓冲区的访问仅仅被守卫通过
QMutex, the consumer thread couldn’t access the buffer at the same time as the producer thread. Yet, there is no harm in having both threads working on 不同部分 的缓冲在同一时间。范例包含 2 个类:
ProducerandConsumer。两者继承自QThread。在这 2 个类之间进行通信所使用的循环缓冲和保护它的同步工具都是全局变量。替代使用
QWaitConditionandQMutex以解决生产者-消费者问题是使用QSemaphore. This is what the 信号量范例 does.
Let’s start by reviewing the circular buffer and the associated synchronization tools:
const int DataSize = 100000; const int BufferSize = 8192; char buffer[BufferSize]; QWaitCondition bufferNotEmpty; QWaitCondition bufferNotFull; QMutex mutex; int numUsedBytes = 0;
DataSizeis the amount of data that the producer will generate. To keep the example as simple as possible, we make it a constant.BufferSizeis the size of the circular buffer. It is less thanDataSize, meaning that at some point the producer will reach the end of the buffer and restart from the beginning.To synchronize the producer and the consumer, we need two wait conditions and one mutex. The
bufferNotEmptycondition is signalled when the producer has generated some data, telling the consumer that it can start reading it. ThebufferNotFullcondition is signalled when the consumer has read some data, telling the producer that it can generate more. ThenumUsedBytesis the number of bytes in the buffer that contain data.Together, the wait conditions, the mutex, and the
numUsedBytescounter ensure that the producer is never more thanBufferSizebytes ahead of the consumer, and that the consumer never reads data that the producer hasn’t generated yet.
Let’s review the code for the
Producer类:class Producer : public QThread { public: Producer(QObject *parent = NULL) : QThread(parent) { } void run() override { for (int i = 0; i < DataSize; ++i) { mutex.lock(); if (numUsedBytes == BufferSize) bufferNotFull.wait(&mutex); mutex.unlock(); buffer[i % BufferSize] = "ACGT"[QRandomGenerator::global()->bounded(4)]; mutex.lock(); ++numUsedBytes; bufferNotEmpty.wakeAll(); mutex.unlock(); } } };生产者生成
DataSizebytes of data. Before it writes a byte to the circular buffer, it must first check whether the buffer is full (i.e.,numUsedBytes等于BufferSize). If the buffer is full, the thread waits on thebufferNotFull条件。At the end, the producer increments
numUsedBytesand signalls that the conditionbufferNotEmptyis true, sincenumUsedBytesis necessarily greater than 0.We guard all accesses to the
numUsedBytesvariable with a mutex. In addition, thewait()function accepts a mutex as its argument. This mutex is unlocked before the thread is put to sleep and locked when the thread wakes up. Furthermore, the transition from the locked state to the wait state is atomic, to prevent race conditions from occurring.
Let’s turn to the
Consumer类:class Consumer : public QThread { Q_OBJECT public: Consumer(QObject *parent = NULL) : QThread(parent) { } void run() override { 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"); } signals: void stringConsumed(const QString &text); };The code is very similar to the producer. Before we read the byte, we check whether the buffer is empty (
numUsedBytesis 0) instead of whether it’s full and wait on thebufferNotEmptycondition if it’s empty. After we’ve read the byte, we decrementnumUsedBytes(instead of incrementing it), and we signal thebufferNotFullcondition (instead of thebufferNotEmptycondition).
在
main(),我们创建 2 线程并调用wait()to ensure that both threads get time to finish before we exit:int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); Producer producer; Consumer consumer; producer.start(); consumer.start(); producer.wait(); consumer.wait(); return 0; }So what happens when we run the program? Initially, the producer thread is the only one that can do anything; the consumer is blocked waiting for the
bufferNotEmptycondition to be signalled (numUsedBytesis 0). Once the producer has put one byte in the buffer,numUsedBytesisBufferSize- 1 and thebufferNotEmptycondition is signalled. At that point, two things can happen: Either the consumer thread takes over and reads that byte, or the producer gets to produce a second byte.The producer-consumer model presented in this example makes it possible to write highly concurrent multithreaded applications. On a multiprocessor machine, the program is potentially up to twice as fast as the equivalent mutex-based program, since the two threads can be active at the same time on different parts of the buffer.
Be aware though that these benefits aren’t always realized. Locking and unlocking a
QMutexhas a cost. In practice, it would probably be worthwhile to divide the buffer into chunks and to operate on chunks instead of individual bytes. The buffer size is also a parameter that must be selected carefully, based on experimentation.