Linux non-locking programming--__sync_fetch_and_add series Atomic manipulation functions

Source: Internet
Author: User

What operations does Linux support have atomic characteristics? Knowing these things is the basis for understanding and designing a non-locking programming algorithm.

The following things are organized from the Web. First of all, thank you for sharing!

__sync_fetch_and_add series of commands, found this series of commands to say the best of an article, English good students can go directly to the original text. Multithreaded simple data type access and atomic variables

__sync_fetch_and_add Series A total of 12 functions, with plus/minus/and/or/XOR//and/or the atomic operation function of the function, __sync_fetch_and_add, as the name implies, fetch first, and then add, return is the previous value of the self-added. Take count = 4 As an example, call __sync_fetch_and_add (&count,1), after which the return value is 4, and then count becomes 5.
There are __sync_fetch_and_add, nature will have __sync_add_and_fetch, hehe this meaning is very clear, the first self-added, in return. The relationship between their two brothers is the same as that of i++ and ++i. By Tanhao strong his old people have received protection fee will be clear.
With this baby function, we have a new solution. For multi-threaded global variables to self-add, we will never have to thread lock. The following line of code is the same as the line of code protected by Pthread_mutex, and is thread-safe.

__sync_fetch_and_add (&global_int, 1);
The following is a family photo of this group of functions, you can see the name to know what these functions are doing.

When compiling with GCC, add the option-march=i686

Sam: On my server, I found no add-on.


type __sync_fetch_and_add (type *ptr, type value);
type __sync_fetch_and_sub (type *ptr, type value);
type __sync_fetch_and_or (type *ptr, type value);
type __sync_fetch_and_and (type *ptr, type value);
type __sync_fetch_and_xor (type *ptr, type value);
type __sync_fetch_and_nand (type *ptr, type value);
type __sync_add_and_fetch (type *ptr, type value);
type __sync_sub_and_fetch (type *ptr, type value);
type __sync_or_and_fetch (type *ptr, type value);
type __sync_and_and_fetch (type *ptr, type value);
type __sync_xor_and_fetch (type *ptr, type value);
type __sync_nand_and_fetch (type *ptr, type value);

Sam: I wonder why we have to write the ellipsis in the back, is there a parameter that doesn't need our attention? Do not need to pass parameters when using? The following two functions are what elder brother wants, can easily realize the function of mutual exclusion lock.

BOOL __sync_bool_compare_and_swap (type*ptr, type oldval, type newval, ...)
Type __sync_val_compare_and_swap (Type *ptr, type Oldval, type newval, ...)
These two functions provide an atomic comparison and exchange, and if *ptr = = Oldval, the newval is written to *ptr,
The first function returns true in the case of equality and writing.
The value of the second function before the return operation.


__sync_synchronize (...)

To understand the above, refer to: http://blog.sunchangming.com/post/47188394133.


There are two other functions:
Type __sync_lock_test_and_set (type *ptr, type value, ...)
Set *ptr to value and return the values before the *ptr operation.

void __sync_lock_release (Type *ptr, ...)
Place the *ptr 0

Linux non-locking programming--__sync_fetch_and_add series Atomic manipulation functions

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.