Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize QT mutex

2025-01-17 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 achieve QT mutex". In the operation of actual cases, many people will encounter such a dilemma, so 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 code of multiple tasks (threads) accessing the same data is executed randomly alternately, which is the reason why the data exchange between tasks (threads) can not be carried out normally. Then the solution is obvious: find a way to ensure that code that accesses the same piece of data does not execute at the same time (intersecting in time).

In fact, there is no way to find a way, because the operating system has prepared this mechanism: mutex (mutex)

Mutex instance (QT environment)

Execution result

Let's change the order today and look at the implementation results first.

WT: >

RT:int DataArray::getDataSize ()

RT: {

RT: return 5

RT:}

RT::----data_size=5

RT:int DataArray::getData (0)

RT: {

RT: if (index > = 0 & & index

< m_dataSize) RT: { RT: return m_buffer[0]RT: } RT:} RT:int DataArray::getData(1) RT:{ RT: if(index >

= 0 & & index

< m_dataSize) RT: { RT: return m_buffer[1]RT: } RT:} RT:int DataArray::getData(2) RT:{ RT: if(index >

= 0 & & index

< m_dataSize) RT: { RT: return m_buffer[2]RT: } RT:} RT:int DataArray::getData(3) RT:{ RT: if(index >

= 0 & & index

< m_dataSize) RT: { RT: return m_buffer[3]RT: } RT:} RT:int DataArray::getData(4) RT:{ RT: if(index >

= 0 & & index

< m_dataSize) RT: { RT: return m_buffer[4]RT: } RT:} RT:----total=10 可以很明显的看出:读和写之间没有任何交叉,当然这时的结果就是对的了。 代码

First create the QMutex object at line 46.

CreatDataTask, which runs from 48 to 68 lines, is an embedded class. Line 57 adds a private data member m_mutex of reference type. The actual object is passed in when line 73 invokes the constructor on line 51. The lock method is called before line 62 accesses the data, and the unlock method is called after line 65 has finished accessing the data.

Reading data is done in the main function, which is also a thread. Similarly, the lock method is called before accessing the data, and the unlock method is called after accessing the data.

This is the end of the content of "how to achieve QT mutex". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report