Go language Atomic atomic operations

Source: Internet
Author: User
Tags cas mutex value store

Atomic is the most lightweight lock, and it is still very effective to use the atomic package directly in some scenarios.

The following excerpt from "Go Concurrent programming Combat"-Atomic operation:
The advantage of CAS operations is that concurrency-safe value substitution operations can be done without forming a critical section and creating mutexes.
This can significantly reduce the loss of synchronization to program performance.
Of course, CAS operations also have disadvantages. CAS operations are not so easy to succeed in situations where the manipulated values are frequently changed.

There are 5 atomic operations, namely: increase or decrease, compare and exchange, load, store and exchange

1. Increase or decrease

The function names that are used to increment or subtract atomic operations (hereinafter referred to as atomic increment/decrement operations) are prefixed with "ADD" and followed by the name of the specific type.
However, due to atomic. The type of the second parameter of the AddUint32 function and the Atomic.adduint64 function is UInt32 and UInt64 respectively, so we cannot reduce the manipulated value by passing a negative value.
Atomic. AddUint32 (&UI32, ^uint32 (-nn-1)) where NN represents a negative integer

2. Compare and Exchange
Func CompareAndSwapInt32 (addr *int32, old, new Int32) (swapped bool)
The value of the first parameter should be a pointer to the value being manipulated. The type of the value is *int32.
The type of the latter two arguments is the int32 type. Their values should represent the old and new values of the manipulated values, respectively.
The CompareAndSwapInt32 function, when called, determines whether the value of the manipulated value pointed to by the parameter addr is equal to that of the parameter old.
The function will replace the old value with the new value represented by the parameter new only if the decision is given a positive result. Otherwise, the subsequent substitution operation is ignored.

3. Loading
V: = Atomic. LoadInt32 (&value)
function Atomic. LoadInt32 accepts a pointer value of type *INT32 and returns the value that the pointer value points to

With the adjective "atomic," it means that at the same time that value is read, no CPU in the current computer will perform any other read or write operations on this value.

Such constraints are supported by the underlying hardware.


4. Storage
In the process of storing a value in an atom, no CPU will perform a read or write operation on the same value.
If we change all writes for this value to atomic operations, then there is no case that the read operation for this value is read to half of the value modified by concurrency.
The atomic value store operation is always successful because it does not care what the old value of the manipulated value is.
function Atomic. StoreInt32 will accept two parameters. The first parameter is of type *int 32, and its meaning is also a pointer to the manipulated value. The second parameter is the Int32 type, and its value should represent the new value to be stored. Other similar functions will also have a list of such parameter declarations.

5. Exchange
Unlike CAS operations, atomic Exchange operations do not care about the old values of the manipulated values. It will set the new value directly. But it's a step more than an atomic loading operation. As an interchange, it returns the old value of the manipulated value. Such operations are less restrictive than CAS operations and are more powerful than atomic loading operations.
To Atomic. SwapInt32 function as an example. It accepts two parameters. The first parameter is the *int32 type value that represents the memory address of the manipulated value, and the second parameter is used to represent the new value. Note that the function has a result value. The value is the old value that was replaced by the new value. Atomic. When the SwapInt32 function is called, the second parameter value is placed on the memory address represented by the first parameter value (that is, the manipulated value is modified), and the previous value on that address is returned as a result.

Example:
Df.rmutex.Lock ()
Defer Df.rmutex.Unlock ()
Return Df.roffset/int64 (Df.datalen)
We now remove the lock and unlock operation that is applied to it, and instead use atomic operations to implement it. The modified code is as follows:
Offset: = Atomic. LoadInt64 (&df.roffset)
Return Offset/int64 (Df.datalen)


Replace a mutex lock with an atomic operation

The main reason is that atomic operations are supported by the underlying hardware, while locks are implemented by the APIs provided by the operating system. The former is usually more efficient if the same functionality is achieved.


With regard to atomic, the author of concurrent programming says it is very clear and can look at the following two good documents:

Golang 1.3 sync. Mutex Source Code parsing

Golang 1.3 sync. Atomic Source Code Analysis


MAIL: [Email protected]

blog:http://blog.csdn.net/xcl168


Go language Atomic atomic operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.