Is it safe to have a static constexpr member variable of a class being read simultaneously by multiple threads? e.g.:
class A{
public:
//some code here
private:
static constexpr std::size_t x_ = 99;
}
If I create multiple objects of type Class A
, each object being used by a different thread, it it safe for each C++11 thread to independently read the member variable x_
without using any locking mechanisms?
Yes, this is safe. A data race happens when you have multiple threads and at least one of them is a writer and you have have no synchronization. If none of your threads are a writer (and you can't, because it is const
) then you can't have a data race.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With