Is it safe to access (read) bits from a bit set (C ++), which can be changed by another thread

Is this type of operation safe? (in other words, there is no way to read some kind of fictitious intermediate value if the bitet changes to another stream)? In particular, I am only interested in whether reading is safe, in other words, I do not ask whether it is safe to record the bitrate from two separate streams.

for example: Will thread 1 reliably receive the current state of bit 5 regardless of whether the same bits are set to bs at the same time?

std::bitset<64> bs;

//thread 1:
bool val;
val=bs.test(5);
// ...

//thread 2:
// set/clear a few bits
bs.set(1);
bs.set(3);
bs.set(5);
bs.reset(6);
// ...
+4
source share
3 answers

Using std::bitset, this method is not thread safe.

(ยง 20.9.2.2-51):

, , .

, ( undefined). .

+1

Bitset , .

reset (5), , 1 .

+1

STL . , , . atomic.

0

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


All Articles