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

atomic_compare_exchange_weak函数


概要:
#include <stdatomic.h>
_Bool atomic_compare_exchange_weak(volatile A *object,
      C *expected, C desired);

描述:

该泛型函数比较参数object指向原子对象的内存内容与参数expected指向对象的内存内容是否相等。如果相等,将使用参数desired的内存内容替换参数object指向原子对象的内存内容;如果不相等,将使用参数object指向原子对象的内存内容更新参数expected指向对象的内存内容。该操作是个原子的读-修改-写操作。


该函数可能会出现伪失败(fail spuriously);即使参数object指向原子对象的内存内容与参数expected指向对象的内存内容相等时,该函数仍可能返回false,并将最初存在的相同内存内容存储回参数expected。这种伪失败可以在更广泛的机器上实现比较和交换操作,例如:LL/SC机(load_locked store_conditional machines)。

伪失败的结果是几乎所有atomic_compare_exchange_weak函数的使用都是在循环中,例如:

expected = atomic_load(&object);
do{
    desired = function(expected);
}while(!atomic_compare_exchange_weak(&object, &expected, desired));

当比较和交换操作在循环中时,atomic_compare_exchange_weak函数和atomic_compare_exchange_weak_explicit函数在某些平台上具有更好的性能。当使用atomic_compare_exchange_weak函数或者atomic_compare_exchange_weak_explicit函数需要循环,但使用atomic_compare_exchange_strong函数或者atomic_compare_exchange_strong_explicit函数不需要循环时,这种情况下atomic_compare_exchange_strong函数和atomic_compare_exchange_strong_explicit函数更可取。


参数:
volatile A *object

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

C *expected

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

C desired

对应的非原子类型对象。


返回值:

函数返回比较结果;如果参数object指向原子对象的内存内容与参数expected指向对象的内存内容相等,函数返回true;如果参数object指向原子对象的内存内容与参数expected指向对象的内存内容不相等,函数返回false


范例:
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 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
/*函数atomic_compare_exchange_weak范例*/

#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>

#define COUNT 10000
#define SIZE 3

atomic_int aNumber;

/*在新线程中执行的函数。*/
int func(void *arg)
{
    for(int i=0; i<COUNT; ++i)
    {
        int before;
        int after;
        
        before = atomic_load(&aNumber);
        do
        {
            after = before + *((int *)arg);
        }while(!atomic_compare_exchange_weak(&aNumber, &before, after));
    }
    
    thrd_exit(0);
}

int main(void)
{
    thrd_t threadId[SIZE];
    int data[SIZE] = {2, 3, 4};
    atomic_init(&aNumber, 0);

    /*创建线程。*/
    for(int i=0; i<SIZE; ++i)
    {
        if(thrd_create((threadId+i), func, (data+i)) != thrd_success)
        {
            perror("thrd_create");
            exit(EXIT_FAILURE);
        }
    }

    /*连接线程。*/
    for(int i=0; i<SIZE; ++i)
    {
        thrd_join(threadId[i], NULL);
    }

    printf("aNumber: %d\n", atomic_load(&aNumber));
    
    return 0;
}


输出:

aNumber: 90000

注:使用Pelles C编译。


相关内容:
atomic_store 替换原子对象值的函数。
atomic_load 读取原子对象值的函数。
atomic_exchange 读取并替换原子对象值的函数。
atomic_compare_exchange_strong 根据比较结果,更新值的函数。
atomic_compare_exchange_weak_explicit 根据比较结果,更新值的函数。