If I understand correctly, every second one thread is reading the voltage, writes it to some "data structure" and other threads are reading from that data structure every now and then (am I correct?)
if this "data structure" has atomic loads and stores (int
,char
, etc. on x86, for example) than it may be possible that the value that other threads are reading will never change (or other nasty things may happen, like reordering). you need synchronization to make sure that atomic store/load is correctly read/written from its memory storage rather than from cached storage.
if this "data structure" is not atomic - then we're dealing with undefined behaviour, and this is wrong always.
so you do need to make the "data structure" both atomic and synchronized, either by atomics, either by locks.
if this "data structure" is small enough, std::atomic
seems suitable here. if not, see if your system supports reader-writer locks, they seems extremly suitable here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…