I have the following code C++ pseudo code
class A
{
private:
B s_staticvar;
public:
static void doSomething()
{
}
}
A number of threads can call doSomething(). All the threads only read s_staticvar & non modify them.
My question is do we have to lock the static var before reading it ?
Can non atomic reads of the object B can cause synchronisation problems ?
You can safely read data from multiple threads. No locking is needed. These thread will not even notice each other. Only you need to ensure that you fully prepare your variable before other threads kick in.
In a multicore/multiprocessor environment it may happen that multiple copies of your data will be present in caches of different processors. This is not a problem as long as they are all the same.