The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 07:03 AM
Access Awk Variables Outside Scope Amruta Pitkar Shell Programming and Scripting 7 01-15-2008 09:17 AM
Access Awk Variables Outside Scope Amruta Pitkar UNIX for Advanced & Expert Users 2 10-26-2006 09:35 PM
C++ variable scope and mutexes Corona688 High Level Programming 0 10-05-2005 01:29 PM
C++: scope, different files etc.. J.P High Level Programming 1 04-25-2002 04:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-23-2006
sundaresh sundaresh is offline
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 ?
  #2 (permalink)  
Old 06-23-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 06-23-2006
sundaresh sundaresh is offline
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.
  #4 (permalink)  
Old 06-23-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 06-23-2006
sundaresh sundaresh is offline
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.
  #6 (permalink)  
Old 06-24-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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 (permalink)  
Old 06-28-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
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..
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0