Thread-shared boolean flag

62 points stunstyle

Suppose I'd like to have a simple boolean flag with which a single thread listening to incoming messages over HTTP can tell all other threads to clean-up and shut down (so something like a SIGTERM). The situation would be such that this flag would only ever be written to by the HTTP thread, and read-only for all others.

​

My first instict was to have a Arc<RwLock<bool>> which works totally fine, but it made me think it's a bit heavyweight for a simple true/false thing.

Looking at the docs I found std::sync::atomic::AtomicBoolbut I am not sure if its usage is totally equivalent and specifying the memory ordering feels a bit raw/error-prone.

So having said all that, my question is: which of these two options would be "best" and if the AtomicBool is the more "optimal" solution, I'd love an example on properly doing read/write ops with it.

​

Edit: Should mention that main target would be an aarch64 platform.