利用Qwaitcondition 控制生产消费者协作


通过分析以下的例子发现:

多线程协作/同步的关键是:如何有效的设计并利用好多个线程之间公共操作的对象,以及相关的变量。

如下 :如果每个线程不共用一个numUsedBytes来标识buff的元素索引的话将会大大的增加消费者和生产者的协作难度。

及利用numUsedBytes来如何控制好 buffer[i % BufferSize] 不会取到生产者还没有向buff中写入的元素


#include <QtCore/QCoreApplication>


#include <QTime>
#include <QtGlobal> 
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <QDebug>


#include <iostream>


using namespace std;


/*
The Wait Conditions example shows how to use QWaitCondition and QMutex to control access to a circular buffer
shared by a producer thread and a consumer thread.
The producer writes data to the buffer until it reaches the end of the buffer,
at which point it restarts from the beginning, overwriting existing data. 
The consumer thread reads the data as it is produced and writes it to standard error.
Wait conditions make it possible to have a higher level of concurrency than what is 
possible with mutexes alone. If accesses to the buffer were simply guarded by a 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 different parts of the buffer at the same time.
The example comprises two classes: Producer and Consumer. Both inherit from QThread.
The circular buffer used for communicating between these two classes and the synchronization tools
that protect it are global variables.
An alternative to using QWaitCondition and QMutex to solve the producer-consumer 
problem is to use QSemaphore. This is what the Semaphores example does.
*/
/*
DataSize is the amount of data that the producer will generate. 
To keep the example as simple as possible, we make it a constant. 
BufferSize is the size of the circular buffer. 
It is less than DataSize, 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 bufferNotEmpty condition is signalled when the producer
has generated some data, telling the consumer that it can start reading it.
The bufferNotFull condition is signalled when the consumer has read some data, 
telling the producer that it can generate more. 
The numUsedBytes is the number of bytes in the buffer that contain data.
Together, the wait conditions, the mutex, and the numUsedBytes counter
ensure that the producer is never more than BufferSize bytes ahead of the consumer, 
and that the consumer never reads data that the consumer hasn't generated yet.
*/
const int DataSize = 5;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const int BufferSize = 3;
char buffer[BufferSize];
QWaitCondition bufferNotEmpty;
QWaitCondition bufferNotFull;
QMutex mutex;
int numUsedBytes = 0;


class Producer : public QThread
{
public:
void run();
static int m_countProducer;
};
int Producer::m_countProducer=0;


void Producer::run()
{


qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

cout<<"producer in run beg count:"<<m_countProducer++<<endl;


for (int i = 0; i < DataSize; ++i) {
mutex.lock();
if (numUsedBytes == BufferSize)
{
// qDebug("producer bufferNotFull.wait(&mutex); --beg");
bufferNotFull.wait(&mutex);
// qDebug("producer bufferNotFull.wait(&mutex); --end");
}
else
{
// qDebug("else producer numUsedBytes =! BufferSize;-- ");
}


// mutex.unlock();
buffer[i % BufferSize] = i+49;//"ACGT"[(int)qrand() % 4];
cout<<endl;
cout<<"producer index:"<< i%BufferSize<<endl;
cout<<"producer i:"<<i<<endl;



// mutex.lock();
++numUsedBytes;
cout<<"producer numUsedBytes:"<<numUsedBytes<<endl;
bufferNotEmpty.wakeAll();
mutex.unlock();
}


int ii=90;
}


/*
The producer generates DataSize bytes of data. Before it writes a byte to the circular buffer,
it must first check whether the buffer is full (i.e., numUsedBytes equals BufferSize).
If the buffer is full, the thread waits on the bufferNotFull condition.
At the end, the producer increments numUsedBytes and signalls that the condition bufferNotEmpty 
is true, since numUsedBytes is necessarily greater than 0.
We guard all accesses to the numUsedBytes variable with a mutex. In addition, 
the QWaitCondition::wait() 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.
*/
class Consumer : public QThread
{
public:
void run();
static int m_countConsumer ;
};


int Consumer::m_countConsumer=0;


void Consumer::run()
{
//cout<<"Consumer in run beg count:"<<m_countConsumer++<<endl;


for (int i = 0; i < DataSize; ++i) {
mutex.lock();
if (numUsedBytes == 0)
{
// qDebug("Consumer bufferNotEmpty.wait(&mutex);-- beg");
bufferNotEmpty.wait(&mutex);
// qDebug("Consumer bufferNotEmpty.wait(&mutex);-- end");
}
else
{
// qDebug("else Consumer numUsedBytes == 0;-- ");
}
// mutex.unlock();


fprintf(stderr, " %c ", buffer[i % BufferSize]);
cout<<endl;
cout<<"Consumer index:"<< i%BufferSize<<endl;
cout<<"Consumer i:"<<i<<endl;

//cout<<endl;
// mutex.lock();
--numUsedBytes;
cout<<"Consumer numUsedBytes:"<<numUsedBytes<<endl;
bufferNotFull.wakeAll();
mutex.unlock();
}
int ill=90;
fprintf(stderr, "\n");
}
/*
The code is very similar to the producer. Before we read the byte,
we check whether the buffer is empty (numUsedBytes is 0) 
instead of whether it's full and wait on the bufferNotEmpty condition if it's empty.
After we've read the byte, we decrement numUsedBytes (instead of incrementing it), 
and we signal the bufferNotFull condition (instead of the bufferNotEmpty condition).
*/




int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Producer producer;
Consumer consumer;
producer.start();
//producer.wait(5000);

consumer.start();
producer.wait();
consumer.wait();
return 0;
return a.exec();
}
/*
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 bufferNotEmpty condition to be signalled (numUsedBytes is 0).
Once the producer has put one byte in the buffer, 
numUsedBytes is BufferSize - 1 and the bufferNotEmpty condition is signalled.
At that point, two things can happen: Either the consumer thread takes over and reads that byte,
or the consumer 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 QMutex has 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.
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值