Note, this is a followup to: What are the options for safely modifying and reading a boolean across multiple threads and cpus?
I have some C++ code that contains roughly this logic:
class wrapper_info {
public:
bool isConnected();
void connectedHandler();
void disconnectedHandler();
protected:
std::atomic<bool> _connected;
}
void wrapper_info::connectedHandler() {
_connected.store(true, std::memory_order_relaxed);
}
void wrapper_info::disconnectedHandler() {
_connected.store(false, std::memory_order_relaxed);
}
bool wrapper_info::isConnected() {
return _connected.load(std::memory_order_relaxed);
}
They are called in the following way:
Thread 1, 2, 3: isConnected()
Thread 2: connectedHandler()
when the connection is initiated.
Thread 3 disconnectedHandler()
when the connection is broken.
Is it sufficient to use memory_order_relaxed? It is my understanding that I would only need others like memory_order_release and memory_order_acquire when I want to ensure that other operations around it are seen by other threads. But I am just setting/reading a value. Thank you lots.