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 ?

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.

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.

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

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.

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.

Are you certain of this? Threads couldn't do much if thread memory wasn't shared! Even seperate processes can share memory..

Threads share process memory - I guess I wasn't clear. But separate instances of a function are instantiated for each thread. This means auto variables declared in the function are local to the thread, ie., they are allocated in the new thread's stack.

Since static variables already exist in BSS are not created anew for each thread.
Any reference to those variables is to the BSS storage which all threads see just the same.

edit - here is a better summary:

Threads in the same process share: 
   Process instructions 
   Initialized global data & static data
   open files (descriptors) 
   signals and signal handlers 
   current working directory 
   User and group id 

Each thread has a unique: 
   Thread ID 
   set of registers, stack pointer 
   stack for local variables, return addresses 
   signal mask 
   priority 
   Return value
   errno value

All of this prolonged discussion is because of the line in red.