Re: static method no locks
- From: Ulrich Eckhardt <doomster@xxxxxxxx>
- Date: Sat, 10 Jun 2006 10:57:09 +0200
Phil Frisbie, Jr. wrote:
jainarunk@xxxxxxxxx wrote:
I have a static method but this function does not make use of locks.<snip>
Then what happens if two threads access the local variables and
parameters values at
different times. What value will each thread see. For example
static means two different things in C whether you are using it with a
function or variable.
For multithreading you only need to worry about static variables,
which are seen by any thread. But your example uses a static function,
which tells the compiler the function is not used outside the scope of
that source file (C) or outside the application (C++). Each thread
that is in that function will have a separate copy of the local
variables, so they will have no interaction with each other.
Oh, wait. Just to clarify a few things about static:
1. file-static
"static int x;" or "static void function(){...}"
These create an unscoped(i.e. lifetime = runtime) variable or function that
have 'internal linkage'. That means that they don't conflict with other
equally named functions/variables in other sourcefiles (translation units,
to be precise). Apart from the linkage, they are just ordinary globals.
[C++] Other than at the global namespace, this can happen in any namespace,
too.
2. function-static
"void function() { static int i; ... }"
This creates another variable with lifetime=runtime, just that it's scope
(i.e. the code from where it can be accessed) is limited. Compared to the
same without 'static', it is not a local variable that is created for
every call to the function so concurrent, consecutive or recursive calls
always have the same object here. One could say it is a global (from
lifetime) with a local scope(restricted to the function).
[C++ only] If the object has a ctor, it will be called the first time the
function is entered.
3. [C++ only] class-static
"class/struct type { static int x; static void function(); };"
Here, the static means that the declared object or function can be used
without an object of the class. Other than that, the object is is of
static duration, just like a global. These effectively create normal
functions/variables (like globals) which are restricted by the
access-specifiers of the class (i.e. they can be
private/protected/public).
Uli
.
- References:
- static method no locks
- From: jainarunk
- Re: static method no locks
- From: Phil Frisbie, Jr.
- static method no locks
- Prev by Date: Re: static method no locks
- Next by Date: Re: Communication between threads from different processes
- Previous by thread: Re: static method no locks
- Next by thread: Re: Locking an STL-like container
- Index(es):
Relevant Pages
|