static member function and thread-safety
Asked Answered
C

6

10

In C++, when you have local variables in a static member function, does it mean those local variables are also implicitly static or are they really local?

example:

static void myClass::somefunc(int someint)
{

int myint = someint;  // is myint really a local variable or does it change due to the static qualifier at function level?


}

Also, different threads from a thread pool running this function, does myint need to be protected by a lock? assuming that all values passed to it are different and have no relation to each other.

EDIT: Thanx for the answers. Now what if I passed in a boost::shared_ptr<T>, knowing that this object would not be concurrently being used by another thread? (Not sure if one can really guarantee that, or can you?)

I guess a raw ptr passed in, would need some protection, if it were being used all over?

Calendre answered 22/12, 2010 at 13:55 Comment(1)
You are confused by static having two completely different meanings. You are not alone, which is perhaps why static on a standalone function is deprecated now.Heliotype
D
10

They are local unless you declare them static - each invokation of the function will have its own copy of the variable and you don't need to to protect them.

Dualistic answered 22/12, 2010 at 13:57 Comment(4)
Some people seem to confuse static functions with static local variables, while there is practically no relation between the two.Trumantrumann
@T33C: You are wrong. The variable is obviously not static (even if the function is). Remove your -1Bushwhack
"... and you don't need to to protect them." - I disagree with this and I am saying that it is unsafe in a multithreaded environment.Delphadelphi
@T33C": How can another thread get concurrent access to a local variable?Dualistic
B
1

myint is local for somefunc and you don't need to protect it across threads.

Bipolar answered 22/12, 2010 at 13:57 Comment(0)
S
1

myint in your example is a local variable, every time somefunc is called myint lives. but not more than that.

myint doesn't need to be protected because its a local variable

Standice answered 22/12, 2010 at 13:58 Comment(0)
E
1

myint will truly be local. You don't have to worry about protecting it. A separate space will be created on the stack for myint for every single function call in memory.

Engineer answered 22/12, 2010 at 13:58 Comment(0)
B
1

The myint variable will stay local, there is no need to protect them since each thread will not share the local variables.

Birdiebirdlike answered 22/12, 2010 at 14:4 Comment(0)
E
1

The static key-word means that the function will not be passed a hidden "this" argument. Also the function will not have access to the class instance data. The static qualifier of function, has no impact on function's local data.

The static RetType SomeClass::SomeMethod(Type arg) has the same "type" as a free functionRetType SomeFunc(Type arg)

Regards,
Marcin

Ewell answered 22/12, 2010 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.