C++ GUI QT 第4版 之线程(二) 线程的同步(4)

 
相当于建立了一些等待队列;
#include <QtCore>
#include <iostream>
 
const int DataSize = 100000;
const int BufferSize = 4096;
char buffer[BufferSize];
 
QWaitCondition bufferIsNotFull;
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();
        while (usedSpace == BufferSize)
            bufferIsNotFull.wait(&mutex);
        buffer[i % BufferSize] = "ACGT"[uint(std::rand()) % 4];
        ++usedSpace;
        bufferIsNotEmpty.wakeAll();
        mutex.unlock();
    }
}
 
class Consumer : public QThread
{
public:
    void run();
};
 
void Consumer::run()
{
    for (int i = 0; i < DataSize; ++i) {
        mutex.lock();
        while (usedSpace == 0)
            bufferIsNotEmpty.wait(&mutex);
        std::cerr << buffer[i % BufferSize];
        --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;
}
 
 
 
 
#include <QApplication>
#include <QtGui>
#include <QtCore>
QWaitCondition ieq10;
QMutex  mutex;
int  i = 0;
class  Thread1: public  QThread
{
public :
     Thread1(){}
private :
    // int i;
protected :
     void  run()
     {
         qDebug() << "thread1 start...\n" ;
         for (;;)
         {
             mutex.lock();
             if (i == 10)
             {
                 ieq10.wakeAll(); //满足条件唤醒2 如果这里不唤醒会有图2的运行结果 //ieq10.wakeAll();
                 mutex.unlock();
                 qDebug() << "thread1 exit...\n" ;
                 break ;
             }
             qDebug() << "i == " <<i<<endl;
             ++ i;
             sleep(1);
             mutex.unlock();
         }
     }
};
class  Thread2: public  QThread
{
public :
     Thread2(){}
protected :
     void  run()
     {
         qDebug() << "thread2 start...\n" ;
         for (;;)
         {
             mutex.lock();
             if (i < 10) //不满足条件一直等待i到10
             {
                 qDebug() << "thread2 waiting..\n" ;
                 ieq10.wait(&mutex);
             }
             qDebug() << "thread2 working..\n" ;
             mutex.unlock();
             qDebug() << "thread2 exit..\n" ;
             break ;
         }
     }
};
 
int  main( int  argc,  char  *argv[])
{
     QApplication a(argc, argv);
     Thread1 t1;
     Thread2 t2;
     t1.start();
     t2.start();
     t1.wait();
     t2.wait();
     return  a.exec();
}
如图所示的运行结果。。当2个线程同时启动是,它们的运行顺序是不确定的;每一次的运行结果都是不一样的
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值