c - C11 stdatomic and calloc -


i've got structure contains atomic field:

#include <stdatomic.h>  struct s {     ...     atomic_int a; }; 

this structure allocated calloc:

struct s *p = calloc(1, sizeof(struct s)); 

is portable expect p->a initialised 0? there enough barriers in code weakly consistent initialisation fine, initial value guaranteed 0?

no, not portable in general. calloc guarantees byte-wise 0 value of underlying object. types (may) have state not equivalent initialization. definitively have use atomic_init put object valid state.

the reason platforms hold "lock" in addition base object because don't implement corresponding assembler instruction. portable need use atomic_var_init or atomic_init atomic objects not statically allocated.

that said, don't know of existing platform need such cruft atomic_int. if platform has atomic_int_lock_free set 2 , sizeof(atomic_int)==sizeof(int), can relatively sure strategy works. test in _static_assert.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -