A/C + + Atom Lock application (cross-platform)

Source: Internet
Author: User

Introduction: Today in the modification of the AMF library found two functions, interlockedincrement (), InterlockedDecrement (), check the data know this is about atomic lock, and this is the system function under Windows, Then the corresponding Linux should also have this function ...


I. Atomic locks under windows

。。。。 adjourned

Two. Atom Lock evolution under Linux

2.1 Online first found atomic_t, Atomic_inc () These functions, and need header file <atomic.h> header file, in fact, this is not good use:


The earliest atomic is the Linux kernel system files, we need to introduce the kernel of the header file, bringing a series of other files introduced, unsuccessful;

Then GNU put atomic_t this set into its own library, when used as long as the introduction of the header file, this is not good use, because the new GNU has removed this set of things, that is, there is no atomic.h this header file;

Finally, GNU adds __sync_add_and_fetch (), a series of functions that perfectly replace the previous function, which is what we need. This set of things is included in the stdlib.h, very convenient.


2.2linux the latest Atom lock

GCC provides the built-in function of the __sync_* series from 4.1.2 to provide atomic operations for addition and subtraction and logical operations.

You can atomically manipulate a numeric type or pointer of 1,2,4 or 8-byte length, which is declared as follows

1.type __sync_fetch_and_add (Type *ptr, type value, ...)
2.type __sync_fetch_and_sub (Type *ptr, type value, ...)
3.type __sync_fetch_and_or (Type *ptr, type value, ...)
4.type __sync_fetch_and_and (Type *ptr, type value, ...)
5.type __sync_fetch_and_xor (Type *ptr, type value, ...)
6.type __sync_fetch_and_nand (Type *ptr, type value, ...)
7. {tmp = *PTR; *ptr op= value; return tmp;}
8. {tmp = *ptr; *ptr = ~tmp & value; return tmp;} Nand
9.
10.type __sync_add_and_fetch (Type *ptr, type value, ...)
11.type __sync_sub_and_fetch (Type *ptr, type value, ...)
12.type __sync_or_and_fetch (Type *ptr, type value, ...)
13.type __sync_and_and_fetch (Type *ptr, type value, ...)
14.type __sync_xor_and_fetch (Type *ptr, type value, ...)
15.type __sync_nand_and_fetch (Type *ptr, type value, ...)
{*ptr op= value; return *ptr;}
. {*ptr = ~*ptr & value; return *ptr;} Nand


The difference between these two sets of functions is that the first group returns the value before the update, and the second group returns the updated value, the following example is quoted here as Http://www.linuxidc.com/Linux/2011-06/37403.htm.

1. #include <stdio.h>
2. #include <pthread.h>
3. #include <stdlib.h>
4.
5.static int count = 0;
6.
7.void *test_func (void *arg)
8.{
9. int i=0;
Ten. for (I=0;i<20000;++i) {
__sync_fetch_and_add (&count,1);
12.}
return NULL;
14.}
15.
16.int Main (int argc, const char *argv[])
17.{
pthread_t ID[20];
int i = 0;
20.
. for (I=0;i<20;++i) {
Pthread_create (&id[i],null,test_func,null);
23.}
24.
for (I=0;i<20;++i) {
Pthread_join (Id[i],null);
27.}
28.
printf ("%d\n", count);
0. return;
31.}


For old code that uses atomic.h, it can be ported to a high-kernel version of a Linux system by means of a macro definition, such as

1. #define ATOMIC_INC (x) __sync_add_and_fetch ((x), 1)
2. #define ATOMIC_DEC (x) __sync_sub_and_fetch ((x), 1)
3. #define ATOMIC_ADD (x, y) __sync_add_and_fetch ((y))
4. #define ATOMIC_SUB (x, y) __sync_sub_and_fetch ((y))



A/C + + Atom Lock application (cross-platform)

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.