当前位置:网站首页>[waiting and wakeup of QT multithreaded threads]
[waiting and wakeup of QT multithreaded threads]
2022-07-26 22:12:00 【Progress every day 2015】
QWatiCondition Of Member functions
·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 )
Parameters :
(1) Release the locked mutex
(2) Blocking time
function :
mutex Will be unlocked , And the calling thread will block , I don't want to come until one of the following conditions is met :
(1) Another thread uses wakeOne() or wakeAll() Transmit to it ;
(2)time Milliseconds past .
wakeAll()
function : Wakes up all waiting threads .
wakeOne()
function : Wake up and wait QWaitCondition One of the threads of .
* Be careful : Here I can't distinguish yet wakeAll() and wakeOne() The difference between
Example 1: Consumers and producers
The main points of :
(1) The producer must check whether the buffer is full (numUsedBytes == BufferSize), If it is , The thread stops and waits bufferEmpty.wakeAll(); If not , The production data , increase numUsedBytes , Activate numUsedBytes ==80
(2) When the program starts running , Only producers can run . Consumers are blocked waiting bufferFull.wakeAll(); Once the producer puts a byte in the buffer , The consumer thread is awakened .
(3) The transition from a locked state to a waiting state is an atomic operation ...( First exposure to atomic operation )
It is mainly divided into 3 Parts of :
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); // The capacity is full , Producer blocking
buffer[i%BufferSize] = i%BufferSize;
numUsedBytes++; // Used to record whether the current buffer is full
bufferFull.wakeAll(); // Buffer has capacity , Wake up consumers
mutex.unlock();
}
}
Comsumer::Comsumer()
{
}
Comsumer::~Comsumer()
{
}
void Comsumer::run()
{
for (int i = 0; i < DataSize; i++)
{
mutex.lock();
if (numUsedBytes == 0)
bufferFull.wait(&mutex); // Capacity is empty , Block consumers
qDebug() << currentThreadId() << buffer[i%BufferSize];
numUsedBytes--;
bufferEmpty.wakeAll(); // The buffer is not full , Wake up producers
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();
} Some doubts about this example ?
This example contains 4 Threads , The main thread , Producer thread , Consumer thread 1, Consumer thread 2;
The problem is : In the producer thread bufferEmpty.wakeAll() Trigger , Automatically unlock() after ; Mutex is in a situation that needs to be preempted , That is, the consumer thread 1 And consumer threads 2 preemption . that , Will there be consumer threads 1 Keep grabbing , and 2 What about the situation that cannot be preempted ?
Here is my program diagram

It was found that the thread was used at the beginning 1 preemption , Then there are threads 1 and 2 Alternate preemption
————————————————
Copyright notice : This paper is about CSDN Blogger 「yxy654538632」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/yxy654538632/article/details/49496561
边栏推荐
猜你喜欢

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

unity 安装失败:operation not permitted, mkdir......

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

Let Xiaobai thoroughly understand performance tuning

【工具】Apifox

If you do not add waitkey() function after imshow() function, it will not be displayed

Implementation of MATLAB short-time autocorrelation

Altium designer 22 modify the layer properties of the selected component

day07-

matlab 画短时能量图
随机推荐
知识库工具 | 微网站、文档中心、形象展示页拖拽即可生成(附模板,直接用)
深入源码剖析String类为什么不可变?(还不明白就来打我)
mysql推荐书
SQL注入 Less24(二次注入)
也谈数据治理
AFNetworking了解
d和c的符区别
Flink's real-time data analysis practice in iFLYTEK AI marketing business
Flink 在讯飞 AI 营销业务的实时数据分析实践
Try new functions | decrypt Doris complex data type array
Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
Props with type Object/Array must...
09.01 深度优先搜索
[shutter -- geTx] pop up - dialog, snackBar, bottomsheet
day07-
matlab 基音周期估计后处理
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
Altium designer 22 Chinese character garbled
npm, npm中文文档, npm学习使用
Jd.com won the highest award for intelligent science and technology in China! Inventory JD system intelligent technology