In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use C++ 's mutex". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The problems faced by
When multiple threads operate a piece of data at the same time, thread scheduling is controlled by the operating system and is carried out at the machine code level, so the code of multiple tasks (threads) accessing the same data is executed randomly and alternately. As a result, the data exchange between tasks (threads) can not be carried out normally.
The way to solve the problem
After the problem is clear, the countermeasure is, of course, to ensure the integrity of the processing of operational data within each thread. But the means to achieve this in CUniverse + is not to protect the data, but to restrict the simultaneous execution of the code that manipulates the data. Specifically, before starting to execute the code that manipulates the data, the mutex is locked through the mutex::lock method, and then the code that operates on the data is executed. After the data manipulation code is executed, the mutex is released through mutex::unlock. Other threads cannot lock the semaphore during the locking of the mutex.
Because the operating system can guarantee that only one thread can lock successfully at the same time, as long as the program locks mutexes before all the code that operates the same data, as a result, it can be guaranteed that only one piece of code that operates the data can be executed at a time without being interrupted by other code that modifies the data. This indirectly ensures the integrity of the data.
Matters needing attention
From the above description, you can see that using Mutex for data interaction must follow the following principles:
All code that manipulates data must first try to lock the same mutex before execution
Code that manipulates data is allowed only if the mutex lock is successful.
After the code that manipulates the data is executed, the mutex must be released
If there is no process of locking mutexes before manipulating data, or if different mutexes are locked, in either case, the goal of only one piece of code execution can not be achieved at the same time.
If the code that manipulates the data does not release the mutex after execution, the subsequent code that processes the data will never be able to lock the mutex, that is, subsequent data processing cannot continue.
Take a look at the code in accordance with the above principles, focusing on mutex:
QMutex mutex
/ / define CreateDataTask class.
Class CreateDataTask: public QThread
{
Public:
CreateDataTask (QMutex& mutex)
: m_mutex (mutex)
{
}
Private:
QMutex& m_mutex
Void run ()
{
For (int I = 0; I < 10; + + I)
{
M_mutex.lock ()
Cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.