c - Creating a counter with atomic_fetch_add_explicit -


#include <stdatomic.h>  void request_number(request_t *request) {     static atomic_int counter;      request->id = atomic_fetch_add_explicit(&counter, 1, memory_order_relaxed);      printf("request assigned id %u\n" request->id); } 

in above c snippet, believe it's ok use memory_order_relaxed, because without memory fences compiler not re-arrange call printf , fetch of request->id, before store of value request->id.

is correct? i'm wanted absolutely sure in case there other things need taken account atomics.

you doing 1 atomic operation , when return have value. else done "normal" memory model sequential code, has been.

the ; @ end of assignment sequence point. approach fine. indeed, thing need atomic operation here is undivided, don't need sequencing guarantees of "normal" atomic operations.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -