The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-23-2006
Registered User
 

Join Date: Jun 2006
Posts: 9
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 ?
Reply With Quote
Forum Sponsor
  #2  
Old 06-23-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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.
Reply With Quote
  #3  
Old 06-23-2006
Registered User
 

Join Date: Jun 2006
Posts: 9
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.
Reply With Quote
  #4  
Old 06-23-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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
Reply With Quote
  #5  
Old 06-23-2006
Registered User
 

Join Date: Jun 2006
Posts: 9
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.
Reply With Quote
  #6  
Old 06-24-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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.
Reply With Quote
  #7  
Old 06-28-2006
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
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..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0