class AtomicFlag

Library: Foundation
Package: Core
Header: Poco/AtomicFlag.h


This class implements an atomic boolean flag by wrapping the std::atomic_flag. It is guaranteed to be thread-safe and lock-free.

Only default-construction is allowed, objects of this class are not copyable, assignable or movable.

Note that this class is not a replacement for (atomic) bool type because its value can not be read without also being set to true. However, after being set (either explicitly, through the set() call or implicitly, via operator bool()), it can be reset and reused.

The class is useful in scenarios such as busy-wait or one-off code blocks, e.g.:

class MyClass { public:

void myFunc()
    if (!_beenHere) doOnce();

void doOnce() { /* executed once */ }

void doMany() { /* executed multiple times */ }


AtomicFlag _beenHere;


MyClass myClass; int i = 0; while (++i < 10) myClass.myFunc();

Member Summary

Member Functions: operator bool, reset, set



AtomicFlag() = default;

Creates AtomicFlag.



~AtomicFlag() = default;

Destroys AtomicFlag.

Member Functions

operator bool inline

operator bool();

Sets the flag to true and returns previously held value.

reset inline

void reset();

Resets the flag to false.

set inline

bool set();

Sets the flag to true and returns previously held value.