Declaring atomic pointers vs. pointers to atomics

前端 未结 1 2006
抹茶落季
抹茶落季 2021-01-23 02:48

I understand that the following declaration creates an array of values, each of which is atomic:

_Atomic int x[10];

However, I\'m unclear on wh

相关标签:
1条回答
  • 2021-01-23 03:03

    It's pointer to atomic integer, see http://en.cppreference.com/w/c/language/atomic .

    To declare atomic pointer to an integer, you would need to put the keyword right before the variable:

     int * _Atomic x;
    

    Note that the example with calloc may work on common platforms but generally there is no warranty that the types of non-atomic and atomic variables are the same. So it's necessary to initialize the variables with atomic_init:

     x = calloc(10, sizeof(_Atomic int));
     for (...) atomic_init(&x[i], 0);
    
    0 讨论(0)
提交回复
热议问题