当前位置:网站首页>Synchronization of QT multithreading
Synchronization of QT multithreading
2022-07-05 08:10:00 【Nanbolwan】
In writing today qt There was a problem in the process of the program . My program theoretically has two threads , But it's not at all . In addition to the main thread, my other thread's main task is to listen to a network port , After receiving the data, send it to the main thread in the form of a signal , Then the main thread processes the corresponding data in the corresponding slot function . At the same time, my main thread also handles many key slot functions , A zero hour dialog window will pop up in the slot function of the key . My problem is when I deal with the pop-up window of the key slot function ( Note that the key slot function has not returned ) The signal sent by the network monitoring thread to the main thread is received on site and enters the corresponding slot function for processing , At this time, the program is stuck . For a professional software engineer , This situation is absolutely not allowed to happen . Based on years of multi-threaded programming experience, I immediately realized the problem ( It is the process described above ). Obviously, this is a typical thread synchronization often encountered in multithreaded programs ( A critical region ) The problem of . Obviously, the code behind the pop-up window is not allowed to be interrupted , We need to protect this part of code as critical area code . So I tried this method , But this method is invalid . So I looked at qt Official documents of , I found the QObject::connect The role of the fifth parameter of the function :
connect(Sender,SIGNAL(signal),Receiver,SLOT(slot),Qt::DirectConnection);
The fifth parameter represents the thread in which the slot function is executed :
1) Auto connect (AutoConnection), Default connection mode , If the signal is connected to the slot , That is, the sender and the receiver are on the same thread , Equivalent to direct connection ; If the sender and receiver are in different threads , Equivalent to a queue connection .
2) Direct connection (DirectConnection), When the signal is transmitted , The slot function immediately calls . No matter which thread the slot function belongs to , Slot functions are always executed on the sender's thread , That is, the slot function and the sender are on the same thread
3) Queue connection (QueuedConnection), When control returns to the event loop of the recipient's thread , The slot function is called . The thread receiver is in the function execution slot , That is, the slot function and the signal receiver are in the same thread
4) Lock queue connection (QueuedConnection)
Qt::BlockingQueuedConnection: The call timing of the slot function is the same as Qt::QueuedConnection Agreement , However, after sending the signal, the sender's thread will block , Until the slot function runs . The receiver and sender must not be in the same thread , Otherwise the program will deadlock . This may be required when synchronization is required between multiple threads .
5) Single connection (UniqueConnection)
Qt::UniqueConnection: This flag It can be done by biting or (|) Used in combination with the above four . When this flag When setting , When a signal is connected to a slot , Repeated connections will fail . That is to avoid repeated connections
therefore , According to the actual application scenario , I used the signal slot function of the network monitoring thread QueuedConnection, Then there is no jam .
边栏推荐
- STM32 tutorial triple ADC interleaved sampling
- Fundamentals of C language
- Interview catalogue
- The research found that the cross-border e-commerce customer service system has these five functions!
- [trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- Compilation warning solution sorting in Quartus II
- Sql Server的存储过程详解
- Problem solving: interpreter error: no file or directory
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
猜你喜欢
![Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine](/img/da/8c70699d2cd3ec5b36ec716b8f6bd1.jpg)
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine

生产中影响滑环质量的因素

Makefile application

NTC thermistor application - temperature measurement

Altium designer learning (I)

Compilation warning solution sorting in Quartus II
![[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen](/img/43/7f8becc09c5ce7fe401bed140608f3.jpg)
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen

The firmware of the connected j-link does not support the following memory access

Ble encryption details

How to copy formatted notepad++ text?
随机推荐
C WinForm [view status bar -- statusstrip] - Practice 2
Tailq of linked list
Reasons for rapid wear of conductive slip rings
UEFI development learning 2 - running ovmf in QEMU
Let's briefly talk about the chips commonly used in mobile phones - OVP chips
Nb-iot technical summary
WiFi wpa_ Detailed description of supplicant hostpad interface
List of linked lists
Bluetooth hc-05 pairing process and precautions
2021-10-28
Management and use of DokuWiki (supplementary)
Volatile of C language
Working principle and type selection of common mode inductor
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Basic information commands and functions of kernel development
Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Simple design description of MIC circuit of ECM mobile phone
Explain task scheduling based on Cortex-M3 in detail (Part 2)
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world