当前位置:网站首页>【Qt多线程之线程的等待和唤醒】
【Qt多线程之线程的等待和唤醒】
2022-07-26 21:28:00 【天天进步2015】
QWatiCondition的成员函数
·QWaitCondition()
·~QWaitCondition()
·bool wait ( QMutex * mutex, unsigned long time = ULONG_MAX )
·void wakeOne ()
·void wakeAll ()QWatiCondition::wait()
·bool wait ( QMutex * mutex, unsigned long time = ULONG_MAX )
参数:
(1)释放锁定的mutex
(2)阻塞的时间
功能:
mutex将被解锁,并且调用线程将会阻塞,直到下列条件之一满足才想来:
(1)另一个线程使用wakeOne()或wakeAll()传输给它;
(2)time毫秒过去。
wakeAll()
功能:唤醒所有等待的线程。
wakeOne()
功能:唤醒等待QWaitCondition的线程中的一个线程。
*注意:这里我暂且还不能分清wakeAll()和wakeOne()的区别
例子1:消费者和生产者
要点:
(1)生产者必须检查缓冲是否已满(numUsedBytes == BufferSize),如果是,线程停下来等待bufferEmpty.wakeAll();如果不是,生产数据,增加numUsedBytes ,激活numUsedBytes ==80
(2)当程序开始运行的时候,只有生产者可以运行。消费者被阻塞等待 bufferFull.wakeAll(); 一旦生产者在缓冲中放入一个字节,消费者线程就被唤醒。
(3)锁定状态到等待状态的转换是原子操作。。。(第一次接触到原子操作)
主要分为3个部分:
main.cpp,WatiCondition.h, WatiCondition.cpp
WatiCondition.h
#pragma once
#include <QThread>
#include <QtDebug>
#include <QMutex>
#include <QWaitCondition>
class Producer :
public QThread
{
public:
Producer();
~Producer();
void run();
};
class Comsumer :
public QThread
{
public:
Comsumer();
~Comsumer();
void run();
};
WatiCondition.cpp
#include "WatiCondition.h"
#include <iostream>
QWaitCondition bufferEmpty;
QWaitCondition bufferFull;
QMutex mutex;
const int DataSize = 1000;
const int BufferSize = 80;
int buffer[BufferSize];
int numUsedBytes = 0;
int rIndex = 0;
Producer::Producer()
{
}
Producer::~Producer()
{
}
void Producer::run()
{
for (int i = 0; i < DataSize; i++)
{
mutex.lock();
if (numUsedBytes == BufferSize)
bufferEmpty.wait(&mutex); //容量已满,生产者阻塞
buffer[i%BufferSize] = i%BufferSize;
numUsedBytes++; //用来记录当前缓冲区是否已满
bufferFull.wakeAll(); //缓冲区有容量,唤醒消费者
mutex.unlock();
}
}
Comsumer::Comsumer()
{
}
Comsumer::~Comsumer()
{
}
void Comsumer::run()
{
for (int i = 0; i < DataSize; i++)
{
mutex.lock();
if (numUsedBytes == 0)
bufferFull.wait(&mutex); //容量为空,阻塞消费者
qDebug() << currentThreadId() << buffer[i%BufferSize];
numUsedBytes--;
bufferEmpty.wakeAll(); //缓冲区未满,唤醒生产者
mutex.unlock();
}
std::cout << std::endl;
}
main.cpp
#include <QCoreApplication>
#include "WatiCondition.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Producer producer;
Comsumer consumerA;
Comsumer consumerB;
producer.start();
consumerA.start();
consumerB.start();
producer.wait();
consumerA.wait();
consumerB.wait();
return a.exec();
}关于这个实例的一些疑惑?
这个例子中含有4个线程,主线程,生产者线程,消费者线程1,消费者线程2;
问题就是:生产者线程中的bufferEmpty.wakeAll()触发,自动unlock()之后 ;互斥量处于需要被抢占的情况,也就是消费者线程1和消费者线程2抢占。那么,会不会出现消费者线程1一直抢占,而2抢占不到的情况呢?
下面是我的程序运行图

发现一开始都被线程1抢占,后面出现了线程1和2交替抢占的情况
————————————————
版权声明:本文为CSDN博主「yxy654538632」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yxy654538632/article/details/49496561
边栏推荐
- Is it safe to open an account on flush? How to choose a securities firm for opening an account
- d和c的符区别
- Go----Go 语言命名规范
- Promise me, don't write shit code after reading it..
- Just one dependency to give swagger a new skin, which is simple and cool~
- Altium Designer 22 修改选中元件的层属性
- yolov1
- SQL注入 Less26(过滤空格和注释符,使用不带空格的报错注入)
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- 现货黄金操作指南与建议(上)
猜你喜欢

In depth analysis of the source code, why is the string class immutable? (hit me before you understand)

matlab 短时自相关实现

正规方程法(Normal Equation)原理以及与梯度下降法的区别

day07-

06 CP command

Unity installation failed: operation not allowed, MKDIR

Schematic diagram of MOS tube

光源控制器拨码开关使用说明

flask 源码启动阶段

Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
随机推荐
Flash source code startup phase
JDBC summary
JS verify complex password
Unity installation failed: operation not allowed, MKDIR
Technology sharing | do you know the functions of the server interface automated testing and requests library?
Implementation of MATLAB short-time autocorrelation
Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?
第15章 mysql用户管理
NPM, NPM Chinese documents, NPM learning and using
Flink's real-time data analysis practice in iFLYTEK AI marketing business
调试stc8a8k64d4单片机485通信总结
Props with type Object/Array must...
Go----Go 语言中的标识符和关键字
Finding a new direction for the development of digital retail is the key to ensure that digital retail can enter a new stage of development
Concept and classification of processes
正则表达式及绕过案例
VB.net Chart1的处理
unity 获取网络时间
JDBC operation and entry case of MySQL
Highlight the secondary and tertiary columns under the primary column of pbootcms