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 use of the atomic weight and memory order in Clover 11?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the use of atomic weight and memory order in Clover 11". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of atomic weight and memory order in Craft 11".

1. The problem of sharing variables in multithreading

(a) iTunes + question

In multithreaded programming, the most frequently cited problem is the well-known ibasket + problem, that is, multiple threads perform inotify + operations on the same shared variable I. The reason for the problem in doing this is that the inotify + operation can be divided into three steps:

Stepoperation1i- > reg (read the value of I to the register) 2inc-reg (increment the value of I in the register) 3regr-> I (write back to memory I)

The above three steps can be spaced apart, not atomic operations, that is, cross-execution of steps may occur when multiple threads are executed at the same time, such as the following:

Stepthread Athread B1I-> reg

2inc-reg

three

I-> reg4

Inc-reg5reg- > I

six

Reg- > I

Assuming that I starts with 0, after executing step 4, both threads assume that the value in the register is 1, and then write it back in steps 5 and 6, respectively. In the end, the value of I after the execution of the two threads is 1. But in fact, we executed iDiffect in two threads, and we had hoped that I would have been 2. ITunes + can actually represent problems such as cross execution in multithreaded programming because the operation is not atomic, but here we only focus on the operation on a single variable.

(B) rearrangement of instructions

Sometimes we use a variable as a flag bit and do something when the variable is equal to a particular value. However, there may still be some unexpected holes, such as two threads executing in the following order:

Stepthread Athread B1a = 1

2 steps = true

three

If flag== true4

Assert (a = = 1)

When B determines that flag is true, it asserts that an is 1, which does seem to be the case. So does it have to be like this? Probably not, because both the compiler and CPU may rearrange instructions (different levels of optimization of the compiler and out-of-order execution of CPU). The actual order of execution may look like this:

Stepthread Athread B1flag = true

two

If flag== true3

Assert (a = = 1) 4a = 1

This rearrangement may lead to the exchange of instructions that do not depend on each other within a thread in order to achieve higher execution efficiency. For example, the above: flag and a do not seem to have any dependency on the A thread, and it seems that the order of execution does not matter. But the problem is that B uses flag as the basis for reading a, and the rearrangement of A's instructions may cause the assertion to fail in step3.

Solution

A relatively safe way is to lock the access to shared variables, which can guarantee mutually exclusive access to the critical area. For example, in the first scenario, if you lock and then unlock, only one thread will be performing iSuppli + operation at the same time. In addition, the locked memory semantics can ensure that the write operation of a thread before releasing the lock must be seen by the later locked thread (that is, happens before semantics), which can avoid reading the wrong value in the second scenario.

What if you think the locking operation is too heavy and you don't want to lock it? Clocked 11 provides some atomic variables and atomic operations to support.

II. The atomic weight of Cobalt 11

The Category 11 standard provides template atomic in the standard library atomic header file to define the atomic weight:

Template

< class T >

Struct atomic

It provides a series of member functions to implement atomic operations on variables, such as read operation load, write operation store, and CAS operation compare_exchange_weak/compare_exchange_strong and so on. For most of the built-in types, Category 11 provides some specializations:

For more types, see std::atomic_bool std::atomicstd::atomic_char std::atomicstd::atomic_schar std::atomicstd::atomic_uchar std::atomicstd::atomic_short std::atomicstd::atomic_ushort std::atomicstd::atomic_int std::atomicstd::atomic_uint std::atomicstd::atomic_long std:: atomic / / http://en.cppreference.com/w/cpp/atomic/atomic

In fact, these specializations are equivalent to an alias, which is essentially the same definition. For the specialization of shaping, there will be some special member functions, such as atom plus fetch_add, atom minus fetch_sub, atom and fetch_and, atom or fetch_or and so on. Common operators + +,--, + =, & =, and so on also have corresponding overloaded versions.

Let's take the int type as an example to solve the problem in the iTunes + scenario we mentioned earlier. First define an atomic weight of type int:

Std::atomic i

Since the atomic weight of int is overloaded with the + + operator, iSense + is an indivisible atomic operation. We use multiple threads to perform iComb + operations for verification. The test code is as follows:

# include # include std::atomic ibot Const int count = 1000000 int Const int n = 10x void add () {for (int j = 0; j < count; + + j) ipassport;} int main () {i.store (0); std::vector workers; std::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.

Share To

Development

Wechat

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

12
Report