1

For example, I have this class:

class Example { public: static int m_test; } 

I have threads A and B both using this static member variable. Is this member variable thread safe somewhere under the hub?

I would assume it is not, since it is statically allocated and therefore both threads would be accessing the same memory location, possibly causing collisions. Is that correct or there is some hidden mechanism that makes this static member thread-safe?

10
  • Depends on what operations you want to do with it. Usually just reading is a single operation. Also writing is a single operation. It gets tricky when you do this for example: m_test++. Commented Jan 18, 2019 at 9:45
  • 1
    @Neijwiert Reading is not necessary a single operation. And even when it indeed is it does not mean that this operation is thread-safe. Commented Jan 18, 2019 at 9:47
  • 1
    @RobertAndrzejuk I don't think it's a duplicate. This question is not about initialization of Example::m_test, it's about its usage from within threads, which may include it's update. Commented Jan 18, 2019 at 9:55
  • 1
    @Neijwiert Yes, but we usually do not write programs that "usually" work :). BTW, even if you are sure that the program will be run on an architecture where loads/stores are atomic, without any kind of synchronization (mutextes, atomic variables, memory fences, ...) you would have no guarantee that the updated value would be "seen" by other threads. A compiler may even optimize out such a write completely. Commented Jan 18, 2019 at 10:01
  • 1
    Ok. But I think it is relevant that initialization is thread safe, which is not mentioned in the answers : stackoverflow.com/questions/8102125/…. Commented Jan 18, 2019 at 10:08

2 Answers 2

2

No it is not thread safe insofar there is no built-in mechanism to obviate data races.

static std::atomic<int> m_test; would be though.

Note that you also have thread_local as a storage duration too - not of use to you in this instance - but if you had that rather than static then every thread would get their own m_test.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe it is thread-safe if the variable is only read by both threads, right? eel.is/c++draft/intro.races#2
@DanielLangr: Yes that is correct - there is no data race in that instance. But you would have to be careful to guard against one thread initialising the value.
Sure, it can be also initialized prior to the existence of the threads. I just wanted to clarify, since OP does not specify whether the variable is read only or written as well.
1

It is safe if both threads just read that variable. If at least one updates it, then it's a data race -> undefined behavior.

Hidden mechanism are atomic operations. E.g., via making this variable of std::atomic<int> type in C++11.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.