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?

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 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. – Suma Dec 22 '10 at 15:17

6 Answers6

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 1
    Some people seem to confuse `static functions` with `static local variables`, while there is practically no relation between the two. – Roman L Dec 22 '10 at 15:04
  • 1
    @T33C: You are wrong. The variable is obviously not static (even if the function is). Remove your -1 – Martin York Dec 22 '10 at 19:38
  • "... 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. – T33C Dec 23 '10 at 09:14
  • 2
    @T33C": How can another thread get concurrent access to a local variable? – sharptooth Dec 23 '10 at 09:27
1

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

ognian
  • 11,451
  • 4
  • 35
  • 33
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

AndersK
  • 35,813
  • 6
  • 60
  • 86
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.

Pace
  • 41,875
  • 13
  • 113
  • 156
1

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

dirac3000
  • 746
  • 6
  • 15
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

Marcin
  • 897
  • 1
  • 7
  • 19