scope


 
Thread Tools Search this Thread
Top Forums Programming scope
# 1  
Old 06-23-2006
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 ?
# 2  
Old 06-23-2006
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  
Old 06-23-2006
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  
Old 06-23-2006
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  
Old 06-23-2006
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  
Old 06-24-2006
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  
Old 06-28-2006
Quote:
Originally Posted by jim mcnamara
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.
Are you certain of this? Threads couldn't do much if thread memory wasn't shared! Even seperate processes can share memory..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

2. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

3. Shell Programming and Scripting

bc: scope doesn't work for me

I am trying to use bc to calculate the difference between two nano second time stamps. bc does the calculation but seems to ignore the scale option: micro_start=$(date +%s.%N) # .. some stuff happens here micro_stop=$(date +%s.%N) TOT=$(echo "scale=3; $micro_stop - $micro_start" | bc)... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

4. Shell Programming and Scripting

Scope of variables between scripts

Friends, I am using ksh under SunoS. This is what I have In file1.sh NOW=$(date +"%b-%d-%y") LOGFILE="./log-$NOW.log" I will be using this file through file1.sh as log file. I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies

5. AIX

`pthread_rwlock_t' was not declared in this scope

Hello All, I am getting this error while compiling my application on IBM AIX 5.3. As I tried to define _XOPEN_SOURCE=500 in makefile, that didn't work. Please help us to resolve the error. (0 Replies)
Discussion started by: mustus
0 Replies

6. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

7. Shell Programming and Scripting

scope of the variable - Naga

Hi All, I am new to unix shell scripting, in the below script "num" is an input file which contains a series of numbers example : 2 3 5 8 I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Discussion started by: nagnatar
13 Replies

8. Shell Programming and Scripting

Variables scope.

Hi , I'm trying to change the variable value in a while loop , however its not working it seems that the problem with subshells while reading the file. #!/bin/sh FLAG=0; cat filename | while read data do FLAG=1; done echo $FLAG Should display 1 instead displays 0 (13 Replies)
Discussion started by: dinjo_jo
13 Replies

9. AIX

Scope of AIX

What is the scope of AIX as I am starting my career as a fresher in AIX administration?? (4 Replies)
Discussion started by: abhishek27
4 Replies

10. Programming

C++: scope, different files etc..

I'm having a problem getting this to work.. I got 3 files, start.C - Where i got my main() function Menu.C & Menu.h - Where I'm trying to use hash_map start.C #include <iostream> #include "Menu.h" using namespace std; int main() { /* test code here */ return 0; } Menu.h ... (1 Reply)
Discussion started by: J.P
1 Replies
Login or Register to Ask a Question