当前位置:网站首页>Correct use of qwaitcondition
Correct use of qwaitcondition
2022-07-24 12:28:00 【Progress every day 2015】
It is easy to use
QWaitCondition Synchronization for multithreading , One thread call QWaitCondition::wait() Block waiting , Until another thread calls QWaitCondition::wake() Wake up and continue to execute .
For the sake of description , It is assumed that the main thread calls Send() Send a packet to the communication port , Then block and wait for the packet to be returned before proceeding . Another thread ( Communication thread ) Continuously receive data from the communication port and parse it into packets , Then wake up the main thread . The following is the simplest method given online :
// Example 1
// The main thread
Send(&packet);
mutex.lock();
condition.wait(&mutex);
if (m_receivedPacket)
{
HandlePacket(m_receivedPacket); // Another thread sends packets back and forth
}
mutex.unlock();
// Communication thread
m_receivedPacket = ParsePacket(buffer); // Parse the received data into packets
condition.wakeAll();
Usually , The above code can run well . But in some special cases , There may be chaos , Greatly reduce the reliability of communication .
In the main thread , call Send(&packet) After sending , If the communication thread immediately receives the packet , It's too late to call in the main thread wait() When , Already first wakeAll() 了 , Obviously, this wake-up is invalid , But the main thread continues to call wait(), And then it's stuck there , Because the package that should be returned has been returned . After testing, the probability of this phenomenon is still very high , Because we can't guarantee that the main thread will always be scheduled first . Even if the main thread has called wait(), Nor can it guarantee the performance of the underlying operating system wait_block The system call precedes wake system call , After all wait() Functions are also encapsulated layer by layer .
Rigorous usage
QWaitCondition::wait() A locked QMutex object . It is necessary to . In the above example 1 code , Although we used mutex, But just for the sake of formality QMutex Parameters , Let the compiler compile normally , in fact , No other thread is used to this mutex. and mutex It is supposed to allow multiple threads to work in coordination , So the above example is for the main thread mutex It's invalid .
according to Qt manual ,wait() Function must pass in a locked mutex object , stay wait() In the process of execution ,mutex Always keep locked , Until the operating system is called wait_block At the moment of blocking mutex Unlock ( Strictly speaking, it should be atomic operation , That is, the system can ensure that it will unlock only when the blocking waiting instruction is actually executed ). After another thread wakes up ,wait() The function will be given again at the first time mutex locked ( This operation is also atomic ), Until the display calls mutex.unlock() Unlock .
It is also used in communication threads mutex after , The whole communication sequence is normal , The problem of example 1 is completely solved . The code is as follows :
// Example 2
// The main thread
mutex.lock();
Send(&packet);
condition.wait(&mutex);
if (m_receivedPacket)
{
HandlePacket(m_receivedPacket); // Another thread sends packets back and forth
}
mutex.unlock();
// Communication thread
m_receivedPacket = ParsePacket(buffer); // Parse the received data into packets
mutex.lock();
condition.wakeAll();
mutex.unlock();
In example 2 above , The main thread first mutex Lock occupied , That is, start from sending packets , Until QWaitCondition::wait() At the operating system level, the blocking wait instruction is really executed , During this period of time of the main thread ,mutex Always locked , Even if the communication thread soon receives the packet , Nor will it directly call wakeAll(), It's calling mutex.lock() Time blockage ( Because the main thread has mutex Occupy locked , Try locking again and you will be blocked ), Until the main thread QWaitCondition::wait() Actually execute the blocking command of the operating system and release mutex, Communication thread mutex.lock() Just then there is a blockage , So let's keep going , call wakeAll(), At this point, the main thread must wake up successfully .
thus it can be seen , adopt mutex Protect the code with strict timing requirements , At the same time wakeAll() Also use the same mutex Protect it , This will guarantee : There must be wait() , There's more wakeAll(), No matter what , Can guarantee this priority , Instead of putting an Oolong .
By extension
mutex and condition Joint use is a common design pattern in multithreading , Not only is Qt, about C++ Of std::condition_variable and std::mutex , as well as java Of synchronized / wait / notify It also applies to .
————————————————
Copyright notice : This paper is about CSDN Blogger 「flyoxs」 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/flyoxs/article/details/54617342
边栏推荐
- 让一套代码完美适配各种屏幕
- Taishan Office Technology Lecture: layout difficulties of paragraph borders
- 微信小程序-绘制仪表盘
- Use of multithreading in QT
- Leetcode:51. queen n
- [mathematical basis of Cyberspace Security Chapter 3] congruence
- Do you regret learning it?
- [leetcode]- linked list-3
- Leetcode-81. search rotation sort array II (binary search returns true/false)
- 02 linear structure 2 multiplication and addition of univariate polynomials (linked list solution)
猜你喜欢

QT notes - realize form adaptation

String matching KMP
![[mathematical basis of Cyberspace Security Chapter 3] congruence](/img/00/42a5f7f6f0e8a50884f4639767949f.jpg)
[mathematical basis of Cyberspace Security Chapter 3] congruence

Aruba learning notes 04 Web UI -- Introduction to configuration panel
![[mathematical basis of Cyberspace Security Chapter 9] finite field](/img/2b/27ba1f3c6ec2ecff4538f9a63a79e8.jpg)
[mathematical basis of Cyberspace Security Chapter 9] finite field

微信小程序生成二维码

Use and expansion of fault tolerance and fusing

Day3: branch structure

Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN

QT notes - EventFilter event filter
随机推荐
Shell script case ---2
thinkphp 实现数据库备份
ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法
Snowflake algorithm (PHP)
4*4图片权重的收敛规则
Markdown mathematical formula syntax
Slow motion animation, window related data and operations, BOM operations [DOM (V)]
VMware virtual machine and vSphere migrate to each other
Qt5.12 + vs2019 cannot locate the program input point in the dynamic link library
How to eliminate the character set and sorting rules specially set for fields in MySQL tables?
微信小程序-绘制仪表盘
[C and pointer Chapter 14] preprocessor
Force deduction exercise - the sum of the kth smallest array in the 21 ordered matrix
计算两个坐标经纬度之间的距离(5种方式)
Ansible的安装及部署
A* and JPS
Aruba learning notes 04 Web UI -- Introduction to configuration panel
Why is there discontinuity in MySQL auto increment primary key?
Miss waiting for a year! Baidu super chain digital publishing service is limited to 50% discount
微信小程序生成二维码