Access to atomic two scalar fields

In a multi-threaded (Linux / amd64, Pthreads, C11) application, I have a struct ( momitem_st in monimelt.h ) containing, in particular, two fields

 uint16_t i_paylkind; void* i_payload; 

(if necessary, I could change the i_paylkind type to some other integral type, for example uintptr_t or atomic_uint )

FWIW, i_paylkind is a discriminant that defines the actual struct type of runtime that i_payload points i_payload .

Is it possible to access these two fields atomically (wrt other threads, referring to the same struct ). Of course, I also have pthread_mutex_t i_mtx; in the same structure that I could lock (using pthread_mutex_lock ). But for performance reasons, I could have avoided this (maybe transactional memory might have been relevant)

It seems that <stdtomic.h> does not provide any mechanisms for this.

(question related to my MELT monitor GPLv3 +)

+6
source share
1 answer

If you really have a C11 compiler, the _Atomic qualifier applies to any data type. Just use it. For struct types, the only thing you can do is load and save, unfortunately you do not have access to individual members.

+4
source

Source: https://habr.com/ru/post/979327/


All Articles