为了高效实现环形缓冲区的生产者和消费者的异步通信操作。我们需要用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.
#include <QtCore> #include <stdio.h> #include <stdlib.h> const int DataSize = 100000 ; const int BufferSize = 8192 ; char buffer [ BufferSize ] ; int numUsedBytes = 0 ; { public : void run ( ) ; } ; void Producer :: run ( ) { 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 ( ) ; } } { 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 [ ] ) { 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:
-
-
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
QSemaphore | QSemaphore(intn=0) | |
QSemaphore | ||
voidQSemaphore | ||
intQSemaphore | available()const | |
voidQSemaphore | ||
boolQSemaphore | tryAcquire(intn=1) | |
boolQSemaphore | tryAcquire(intn , inttimeout) |
#include <QtCore> #include <stdio.h> #include <stdlib.h> const int DataSize = 100000 ; const int BufferSize = 8192 ; char buffer [ BufferSize ] ; { public : void run ( ) ; } ; void Producer :: run ( ) { for ( int i = 0 ; i < DataSize ; ++ i ) { freeBytes . acquire ( ) ; buffer [ i % BufferSize ] = "ACGT" [ ( int ) qrand ( ) % 4 ] ; usedBytes . release ( ) ; } } { 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 [ ] ) { Producer producer ; Consumer consumer ; producer . start ( ) ; consumer . start ( ) ; producer . wait ( ) ; consumer . wait ( ) ; return 0 ; }
FROM: http://blog.163.com/qimo601@126/blog/static/1582209320121118112828662/