当前位置: C语言 -- 标准库 -- <stdatomic.h> -- atomic_fetch_and

atomic_fetch_and函数


概要:
#include <stdatomic.h>
C atomic_fetch_and(volatile A *object, M operand);

描述:

该泛型函数将参数object指向的原子对象值与参数operand进行按位与运算,得到的结果替换参数object指向的原子对象值。该操作是个原子的读-修改-写操作。该函数适用于原子整数类型对象,但不包括atomic_bool类型对象。

对于有符号整数类型,算术定义使用二进制补码形式表示,并且溢出时进行安静绕回处理(silent wrap-around on overflow);不存在未定义结果。对于地址类型,结果可能是未定义地址,但操作没有未定义行为。


该泛型函数和复合赋值运算符&=几乎等价,区别主要在于:

1、复合赋值运算符&=不能保证是原子操作。

2、复合赋值运算符&=产生的值是对象的更新值;但该泛型函数返回值是原子对象的先前值。


参数:
volatile A *object

指向原子类型对象的指针。

M operand

与参数object指向原子对象值进行按位与运算的操作数。如果参数object是原子整数类型,参数operand为对应的非原子类型;如果参数object是原子指针类型,参数operandptrdiff_t类型。


返回值:

函数返回函数调用生效前参数object指向的原子对象值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
/*函数atomic_fetch_and范例*/

#ifdef __STDC_NO_ATOMICS__
#error "Implementation does not support atomic types."
#endif

#ifdef __STDC_NO_THREADS__
#error "Implementation does not support multi-threads."
#endif

#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

atomic_int aNumber = 7;

/*新线程中执行的函数。*/
int func(void *arg)
{
    puts("Before operation");
    printf("aNumber: %d\n", atomic_fetch_and(&aNumber, 5));
    puts("After operation");
    printf("aNumber: %d\n", atomic_load(&aNumber));
    
    thrd_exit(0);
}

int main(void)
{
    thrd_t threadId;

    if(thrd_create(&threadId, func, NULL) != thrd_success)
    {
        perror("thrd_create error");
        exit(EXIT_FAILURE);
    }

    thrd_join(threadId, NULL);
    
    return 0;
}


输出:

Before operation

aNumber: 7

After operation

aNumber: 5

注:使用Pelles C编译。


相关内容:
atomic_fetch_add 原子加运算函数。
atomic_fetch_sub 原子减运算函数。
atomic_fetch_or 原子按位或运算函数。
atomic_fetch_xor 原子按位异或运算函数。
atomic_fetch_and_explicit 原子按位与运算函数。