QThread::wait

1.QThread阻塞创建线程对象的线程(一般是主线程)

class MyThread : public  QThread{ 
Q_OBJECT
public:

       MyThread(QObject *parent = NULL);
       ~MyThread();
protected:
       void run();
};
void MyThread::run(){
 		qDebug( "finish!");
 }

使用

MyThread thread;
thread.start();
thread.wait();   //thread是主线程创建所以这里阻塞的是主线程

当然我们先开的线程可能是要运行很久,会卡住主线程,使用QeventLoop就可以轻松解决此类问题:

 
MyThread thread;
thread.start();
QeventLoop;
connect(&thread,SIGNAL(finished ()),&eventLoop,SLOT(quit()));
thread.wait(1);
eventLoop.exec();

2.QMutex mutex; //使用互斥量保证对线程操作的原子性

mutex.lock();
//业务代码
mutex.unlock(); 

QWaitCondition是用来同步线程的条件变量

/*mutex将被解锁,并且调用线程将会阻塞,直到下列条件之一满足才想来:
(1)另一个线程使用wakeOne()或wakeAll()传输给它;
(2)time毫秒过去。*/
wait ( QMutex * mutex, unsigned long time = ULONG_MAX )
/**唤醒所有等待的线程,线程唤醒的顺序不确定,由操作系统的调度策略决定***/
wakeAll ()
/**唤醒等待QWaitCondition的线程中的一个线程,线程唤醒的顺序不确定,由操作系统的调度策略决定**/
wakeOne()

//例子:


 全局配置
const int DataSize = 127;
 
const int BufferSize = 8192;
char buffer[BufferSize];
 
QWaitCondition bufferNotEmpty;
QWaitCondition bufferNotFull;
QMutex mutex;
int numUsedBytes = 0;
 

 //生产
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] = i;//"ACGT"[QRandomGenerator::global()->bounded(4)];
 
            mutex.lock();
            ++numUsedBytes;
            bufferNotEmpty.wakeAll();
            mutex.unlock();
        }
    }
};
 
 //消费
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, "%d\n", buffer[i % BufferSize]);
 
            mutex.lock();
            --numUsedBytes;
            bufferNotFull.wakeAll();
            mutex.unlock();
        }
        fprintf(stderr, "\n");
    }
 
signals:
    void stringConsumed(const QString &text);
};
 

int main(int argc, char *argv[])
 
{
    QCoreApplication app(argc, argv);
    Producer producer;
    Consumer consumer;
    producer.start();
    consumer.start();
    producer.wait();
    consumer.wait();
    return 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁晓山(ben)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值