![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Variables scope. | dinjo_jo | Shell Programming and Scripting | 13 | 09-10-2008 04:03 AM |
| Access Awk Variables Outside Scope | Amruta Pitkar | Shell Programming and Scripting | 7 | 01-15-2008 06:17 AM |
| Access Awk Variables Outside Scope | Amruta Pitkar | UNIX for Advanced & Expert Users | 2 | 10-26-2006 06:35 PM |
| C++ variable scope and mutexes | Corona688 | High Level Programming | 0 | 10-05-2005 10:29 AM |
| C++: scope, different files etc.. | J.P | High Level Programming | 1 | 04-25-2002 01:41 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
scope
Each thread has a copy of auto variables within a function, but variables
declared as static within a function are common to all threads. To circumvent this can static variables be placed outside the function. If so, will the scope of the variable be file only or will it be extern, and will each thread invoking the function receive a copy of the variable ? |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
static globals have a scope of "file". Each thread will not get it's own copy.
In short, DO NOT use static variables for thread programming. |
|
#3
|
|||
|
|||
|
scope
I mean , variables not declared as static within the file, but are static within
the function, for instance, int seed, times; void srand(int s) { seed = s; times = 0; } int rand() { static rand; if(! times) rand = seed; return rand = ((seed * rand) + (++times)) % INT_MAX; } Clearly rand() is not thread safe. Will placing the variable rand outside, along with seed and times make it thread safe ? How else can rand() be made thread safe ? Also will seed, and time have file scope alone or will they be extern and have program scope. |
|
#4
|
|||
|
|||
|
Okay. Let me try again. First, static variables and multithreading do not make good neighbors.
static variables for a function are allocated ONCE, not on the stack like automatic variables, but in one single separate area called the BSS. That means every thread accesses the same memory location for a static variable. automatic variables exist as separate copies for each invocation (thread) of the function. So there are mutltiple copes of those variables with function scope only. That means threads can't stomp on each other's data and trash what your code is trying to do, which is what happens with static varaibles. Read this link and maybe you will see what I'm trying to say: http://wearcam.org/ece385/lecture5/cexamples/bss.htm |
|
#5
|
|||
|
|||
|
scope
Which does'nt answer my question. I think that model you pointed to is
independent of threads. So will there be several variables, one for each thread, of the seed and times variables or just one variable common to all these threads. If the data and BSS segment can change size, then multiple copies of seed and times can exist depending on the need, so the functions will be thread safe, but if these segments cannot change size, then these functions will not be thread safe.If these segments can grow like a stack, then every invocation of the function can get a local copy of these variables , else there is no other alternative but to maintain an array of these variables. int seed[PTHREAD_MAX], times[PTHREAD_MAX], rand[PTHREAD_MAX]; void srand(int s) { int self; self = pthread_self(); seed[self] = s; times[self] = 0; } int rand() { int self; self = pthread_self(); if(! times[self]) rand[self] = seed[self]; return rand[self] = ((seed[self] * rand[self]) + (++times[self])) % INT_MAX; } This is provided pthread_self() always returns an ordinal, between 0 and PTHREAD_MAX for each process. |
|
#6
|
|||
|
|||
|
There is one and only one copy of BSS in memory. You can think of each thread, in a conventional POSIX thread environment, as memory allocated to a function that is scheduled separately, as if it were a process. If, for example, you're on Linux prior to kernel 2.6, then this is not true, it's more like separate processes for each thread, with multiple copies of BSS.
The scope of a given static variable for each of the threads is global - there is only one copy of the static variable. Every thread uses the same memory location to access that static. Unless you run your code under an environment that doesn't fully implement POSIX threading. If you need "static" consider using shared memory and a mutex. |
|
#7
|
|||
|
|||
|
Quote:
|
|||
| Google The UNIX and Linux Forums |