Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static constexpr class member variable safe for multithreading reads?

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?

like image 526
astrophobia Avatar asked Oct 17 '25 06:10

astrophobia


1 Answers

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.

like image 114
NathanOliver Avatar answered Oct 19 '25 20:10

NathanOliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!