Re: static method no locks



Phil Frisbie, Jr. wrote:
jainarunk@xxxxxxxxx wrote:
I have a static method but this function does not make use of locks.

Then what happens if two threads access the local variables and
parameters values at
different times. What value will each thread see. For example
<snip>

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

.



Relevant Pages

  • Re: PEP-343 - Context Managment variant
    ... > def Synhronised: ... 'globals' means the module global namespace as returned by globals. ... We must declare local vars, to which we wish assign in "global" ... You cannot rebind enclosed local variables, ...
    (comp.lang.python)
  • Re: Global Variables
    ... > using globals is to prevent accidental change.. ... Simply beeing careful doesn't help. ... By using local variables you draw a border. ... It is like a house: if you don't make any window or doors in your house, ...
    (comp.lang.cpp)
  • Re: Global Database Variable
    ... Not only are local variables safer and less likely to be affected by other ... globals are lost whenever you "reset" during development? ... > Dim dbs As DAO.Datbase ...
    (microsoft.public.access.forms)
  • Re: PyLint results?
    ... are warnings or what I can do to fix them: ... The id can you get if you enable them in the output via cmdline or config ... Globals should be discouraged, but often they can't be avoided. ... What is wrong with too many local variables? ...
    (comp.lang.python)
  • Re: Newbie Q: Declare variable IN the loop or BEFORE the loop?
    ... when in fact it's the other local variables that they (via the ... anonymous delegates) reference that are of concern. ... and "expensive" are tied in lifetime by their being in the same scope, ...
    (microsoft.public.dotnet.languages.csharp)