Reputation: 79
I'm trying to understand the details in the TCB (thread control block and the differences between per-thread states and shared states. My book has its own implementation of pthread, so it gives an example with this mini C program (I've not typed the whole thing out)
#include "thread.h"
static void go(int n);
static thread_t threads[NTHREADS];
#define NTHREADS 10
int main(int argh, char **argv) {
int i;
long exitValue;
for (i = 0; i < NTHREADS; i++) {
thread_create(&threads[i]), &go, i);
}
for (i = 0; i < NTHREADS; i++) {
exitValue = thread_join(threads[i]);
}
printf("Main thread done".\n);
return 0;
}
void go(int n) {
printf("Hello from thread %d\n", n);
thread_exit(100 + n);
}
What would the variables i
and exitValue
(in the main()
function) be examples of? They're not shared state since they're not global variables, but I'm not sure if they're per-thread state either. The i
is used as the parameter for the go function when each thread is being created, so I'm a bit confused about it. The exitValue's scope is limited only to main()
so that seems like it would just be stored on the process' stack. The int n
as the parameter for the void go()
would be a per-thread variable because its value is independent for each thread. I don't think I fully understand these concepts so any help would be appreciated! Thanks!
Upvotes: 2
Views: 2031
Reputation: 10063
All of the variables in your example program are automatic variables. Each time one of them comes into scope storage for it is allocated, and when it leaves its scope it is no longer valid. This concept is independent of whether the variables is shared or not.
The scope of a variable refers to its lifetime in the program (and also the rules for how it can be accessed). In your program the variables i
and exitValue
are scoped to the main
function. Typically a compiler will allocate space on the stack which is used to store the values for these variables.
The variable n
in function go
is a parameter to the function and so it also acts as a local variable in the function go
. So each time go
is executed the compiler will allocate space on the stack frame for the variables n
(although the compiler may be able to perform optimization to keep the local variables in registers rather than actually allocating stack space). However, as a parameter n
will be initialized with whatever value it was called with (its actual parameter).
To make this more concrete, here is what the values of the variales in the program would be after the first loop has completed 2 iterations (assuming that the spawned threads haven't finished executing).
Main thread: i = 2, exitValue = 0
Thread 0: n = 0
Thread 1: n = 1
The thing to note is that there are multiple independent copies of the variable n
. And that n
gets a copy of the value in i
when thread_create
is executed, but that the values of i
and n
are independent after that.
Finally I'm not certain what is supposed to happen with the statement exitValue = thread_join(threads[i]);
since this is a variation of pthreads. But what probably happens is that it makes the value available when another thread calls thread_join
. So in that way you do get some data sharing between threads, but the sharing is synchronized by the thread_join
command.
Upvotes: 1
Reputation: 215287
They're objects with automatic storage, casually known as "local variables" although the latter is ambiguous since C and C++ both allow objects with local scope but that only have one global instance via the static
keyword.
Upvotes: 0