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

What is the sequence diagram of thread synchronization and data exchange in computer programming?

2025-01-19 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 "what is the sequence diagram of thread synchronization and data exchange in computer programming". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!

Performance in UML

In addition to code, multithreading can also be represented by UML diagrams, and this article uses sequence diagrams to illustrate the previous example.

Multithread safe data classes

First, let's take a look at the example of a multithread-safe data class.

In this figure, the CreateDataTask is first created by the Main function and the thread is started. Note that this is an asynchronous call.

There is a concurrent series of diagram fragments showing the setData operation issued from the run method of CreateDataTask and the removeData operation issued from the Main function. Both operations are in the same critical section of DataArray and cannot be performed at the same time.

In this example, there is no synchronization between the threads accessing the data, and the security of the data is ensured by the DataArray class.

Thread synchronization

Unlike thread-safe data classes, the sequence diagram of thread synchronization is as follows:

In this case, the part that creates the thread object is the same as the part that starts the thread. Unlike the first example, this thread uses thread synchronization to keep the data safe by controlling the pace of reading and writing data.

Specifically, there are two asynchronous signal messages, dataReady and bufferReady. Take dataReady as an example, the settings are as follows:

The steps for data exchange are as follows:

After WriteDataTask writes data to DataArray, it sends a dataReady signal to the Main function (for the first time only)

After receiving the dataReady signal, the Main function takes the data and accepts the bufferReady signal sent to the WriteDataTask.

After receiving the dataReady signal, WriteDataTask writes data to the DataArray and then sends the dataReady signal to the Main function

After receiving the dataReady signal, the Main function takes the data and accepts the bufferReady signal sent to the WriteDataTask.

....

Since the threads for reading and writing data have been synchronized, DataArray is not required to be multithread safe in this example.

In addition, the implementation of the signal uses QMutex, as you can refer to the code in the previous example.

Be careful

In the actual development, both forms exist and need to be chosen according to the specific situation.

This is the end of the content of "Thread synchronization and data Exchange sequence Diagram in computer programming". 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