Reputation: 995
That is what's the scope of sharing static members?
Upvotes: 0
Views: 154
Reputation: 69997
Yes, static
storage duration implies that the variable in question comes into existence when the process is started and is deallocated not before the end of the process. It is shared by all threads of the process, and accessing it can cause data races between the threads, just like with a global variable.
C++11 introduced a new storage duration specifier thread_local
, the use of which implies that there is one instance of the variable in each individual thread. It is allocated when the thread begins.
Unfortunately none of the major compilers (GCC, Clang, VC++) has implemented this fully yet.
Upvotes: 2
Reputation: 4602
Yes, a class static
member is shared across all instances of that class. It's scope can be restricted by marking it public
, protected
or private
. If you are going to have multiple threads simultaneously accessing/mutating the static
member then you will need to synchronise this access, e.g. via mutexes.
Upvotes: 2
Reputation: 258618
Yes, threading doesn't influence static storage. You can think of static
members as globals. So modifying a static
is not thread-safe, something to think about.
Upvotes: 1