How does C17 want me to initialize my atomics?

我只是一个虾纸丫 提交于 2020-01-13 08:30:36

问题


The C17 standard deprecates ATOMIC_VAR_INIT from stdatomic.h, meaning it still supports it but would rather it not be used. What is the correct non-deprecated way of initializing atomics in C17?

Same as non-atomic types:

atomic_int foo = 42;

Or something new?


回答1:


C17 makes it ok to initialize atomics using the usual explicit initialization:

atomic_int n = 42;

C17 literally just dropped the two words "using ATOMIC_VAR_INIT" from the sentence in 7.17.2.1.




回答2:


Based on that document, section DR 454, using the macro makes it impossible to know in which state is the variable.

atomic_int guide1 = ATOMIC_VAR_INIT(42); /* known value(42); WHAT STATE? */

But using the normal assignment is also undetermined, as shown bellow.

atomic_int guide2;        /* indeterminate value; indeterminate state */
atomic_int guide3 = 42;   /* known value(42); indeterminate state */

To put your variable in a known state, you have either to use static or the atomic_init function.

static atomic_int guide4;  /* known value(0); valid state */
static atomic_int guide5 = 42; /* known value(42); valid state */
atomic_int guide6;
atomic_init(&guide6, 42); /* known value(42); initialized state */

But that's the only information I could find.



来源:https://stackoverflow.com/questions/48841767/how-does-c17-want-me-to-initialize-my-atomics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!