I am using C++ std::atomic_flag
as an atomic Boolean flag. Setting the flag to true or false is not a problem but how to query the current state of flag without setting it to some value? I know that there are methods 'atomic_flag_clear
' and 'atomic_flag_set
'. They do give back the previous state but also modify the current state. Is there any way to query flag state without modifying it or do I have to use full fledged 'std::atomic<bool>
'.

- 5,493
- 10
- 37
- 49
-
1It would appear that its only use is to be a means of acquiring a lock. You could use a `std::atomic_flag` as a lock which you could acquire before accessing a shared resource, but if that shared resource is just a `bool` then as you said, you may as well just used `std::atomic
`. _Edit:_ or rather std::atomic_bool, seeing as how they've gone to the trouble of specialising one for you! – Rook Jun 19 '12 at 11:15 -
5You can't know the current state, unless you use the correct acquire/release semantics (i.e. try to lock it with `atomic_flag_set`, and then only do stuff if you actually changed the value). Simply reading it would only tell you what the value was when you read it, and it might have changed immediately afterwards. – Mike Seymour Jun 19 '12 at 11:20
-
@Mike: does that actually matter though? Sounds like all the OP wants is the ability to perform an atomic read and have some notion of ordered reads and writes. – Rook Jun 19 '12 at 11:52
-
@Rook You are right. I want to know the status at the time of read. Depending on that result I take some action. Basically in Java they have 'atomicboolean' which allows get and set 'state'. – polapts Jun 19 '12 at 12:19
-
You might also find [this other question illuminating](http://stackoverflow.com/questions/9806200/how-to-atomically-negate-an-stdatomic-bool). Anyway, I think that `std::atomic_bool` and its `load()` method are probably what you want. – Rook Jun 19 '12 at 12:26
-
`std::atomic_flag` is the only atomic type which is guaranteed to be lock free according to standard. So I was inclined to use it. Not able to query the state information is hampering the use of it. – polapts Jun 19 '12 at 12:35
-
C++20 introduces [`test`](https://en.cppreference.com/w/cpp/atomic/atomic_flag/test) – Matt Eding Oct 28 '20 at 21:20
3 Answers
You cannot read the value of a std::atomic_flag
without setting it to true
. This is by design. It is not a boolean variable (we have std::atomic<bool>
for that), but a minimal flag that is guaranteed lock free on all architectures that support C++11.
On some platforms the only atomic instructions are exchange instructions. On such platforms, std::atomic_flag::test_and_set()
can be implemented with exchange var,1
and clear()
with exchange var,0
, but there is no atomic instruction for reading the value.
So, if you want to read the value without changing it, then you need std::atomic<bool>
.

- 66,628
- 14
- 133
- 155
-
What if I only want to print it's value for debugging purpose, so I don't really care that *this* particular read is atomic? I now use the `.__val` element for that, but it gives in error in some versions of gcc (rightfully so I guess) – user1273684 Jan 29 '18 at 23:44
-
1You can't legitimately read the value of a `std::atomic_flag` for any purpose without modifying it. If you want to read it (even for printf debugging), you need `std::atomic
` – Anthony Williams Feb 01 '18 at 18:30
If you want to use atomic_flag
to determine whether a thread should exit, you can do it like this:
Initialization:
std::atomic_flag keep_running = ATOMIC_FLAG_INIT;
keep_running.test_and_set();
Thread loop:
while (keep_running.test_and_set()) {
// do thread stuff
}
When you want the thread to exit:
keep_running.clear();

- 30,272
- 27
- 92
- 113
-
3Wow, I can't believe I didn't realize this possibility, to use the flag the other way around. – user1768761 Apr 09 '20 at 13:41
-
I know the question is flagged with `c++11`, but it is also very old. Just added for completeness. – pogojotz Feb 17 '21 at 16:35