Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of Linux lock-free examples

Detailed explanation of Linux lock-free examples

零下一度
Release: 2017-07-03 15:47:45
Original
1362 people have browsed it

linuxWhich operations supported are atomic? Knowing these things is the basis for understanding and designing lock-free programming algorithms.

The following things are compiled from the Internet. Thank you in advance for sharing!

sync_fetch_and_add series of commands, I found the best article about this series of commands. Students who are good at English can go directly to the original text. Multithreaded simple data type access and atomic variables

sync_fetch_and_add series has a total of twelve functions, including addition/subtraction/AND/OR/XOR/etc. Atomic operation function, sync_fetch_and_add, as the name suggests, fetch first, then add it, and return the value before adding it. Taking count = 4 as an example, after calling sync_fetch_and_add(&count,1), the return value is 4, and then the count becomes 5.
With sync_fetch_and_add, naturally There is sync_add_and_fetch. Haha, the meaning of this is very clear. Add it first and then return. The relationship between the two brothers is the same as the relationship between i++ and ++i. Anyone who has been raped by Tan Haoqiang or received protection money from his elders will know it clearly.
With this baby function, we have a new solution. For multi-threads to self-increment the global variable , we no longer need to worry about thread locks. The following line of code has the same effect as the line of code protected by pthread_mutex above, and it is also thread safe.

sync_fetch_and_add( &global_int, 1 );
The following is a family portrait of this group of functions. You can know what these functions do by looking at their names.

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

// sam:在我的服务器上,发现不加都可以。
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);
Copy after login

// sam: I’m really wondering why the ellipsis is written after it. Are there any parameters that we don’t need to care about? No need to pass parameters when using it? The following two functions are exactly what I want, and they can easily implement the mutex lock function.

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 atomic comparison and exchange. If *ptr == oldval, write newval to *ptr,
th One function returns true if equal and written.
The second function returns the value before the operation.

sync_synchronize (...)

To understand the above, please refer to: blog.sunchangming.com/post/47188394133
There are also two functions:
type sync_lock_test_and_set (type *ptr, type value, ...)
Set *ptr to value and Returns the value before *ptr operation.
void sync_lock_release (type *ptr, ...)
Set *ptr to 0

The above is the detailed content of Detailed explanation of Linux lock-free examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template