Today we came accross a problem concerning static member functions in an multithreaded environment. The question we asked ourselves and couldn’t find a satisfying answer is:
are local varialbes of static member functions static as well?
// header
class A
{
static int test();
}
// implementation
int A::test()
{
int a = rand();
int b = rand();
int c = a + b;
return c;
}
Say you have two threads both calling A::test(). Is it possible that while thread 1 proccesses c = a + b thread 2 enters test() and changes the value of a by assigning the new return value of rand() or in other words do both threads operate an the some memory locations for a, b and c?
No. The stack frames are independent for each thread’s invocation of the function, and each gets its own locals. (You do need to be careful if you’re accessing actual shared data e.g. static members in the class.)