I read that threads share the memory address space of it's parent thread. If that is true , why can't a thread function access a local variable belonging to it's parent thread ?
void* PrintVar(void* arg){
printf( "%d\n", a);
}
int main(int argc, char*argv[]) {
int a;
a = 10;
pthread_t thr;
pthread_create( &thr, NULL, PrintVar, NULL );
}
If the thread shares the address space , then the function PrintVar should have been able to print the value of variable a
, right ?
I read this piece of info on http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Threads in the same process share: Process instructions Most data open files (descriptors) signals and signal handlers current working directory User and group id
If that is true, then why does int a
not qualify as a shared variable ?
I'd like to see an example code where the file descriptors are shared
The child thread can access the variable in the stack of the parent thread, it just needs to know the variable's address. For example, you could do it like this:
void* PrintVar(void* arg){
int * a = (int *) arg;
printf( "%d\n", *a);
}
int main(int argc, char*argv[]) {
int a;
a = 10;
pthread_t thr;
pthread_create( &thr, NULL, PrintVar, &a );
}
Note that this sort of thing can be tricky, since you have to (one way or another) guarantee that (a) won't get destroyed while the child thread is still accessing it. (In this case you'd probably want to call pthread_join() at the end of main(), so that the main thread will block there and not return from main() until after the child thread has exited)
you couldn't do that even if this were not a thread, because a is out of scope.
put a in the global scope, like so:
int a;
void* PrintVar(void* arg){
printf( "%d\n", a);
}
int main(int argc, char*argv[]) {
a = 10;
pthread_t thr;
pthread_create( &thr, NULL, PrintVar, NULL );
}
This is actually not an issue of threads. consider the following code:
void PrintVar(){
printf( "%d\n", a);
}
int main(int argc, char*argv[]) {
int a;
a = 10;
PrintVar();
}
This obviously won't work, because the variable name a
declared in main
is not visible in PrintVar
, because it's in the local scope of another block. This is a compile-time problem, the compiler just doesn't know what you mean by a
when you mention it in PrintVar
.
But there is also another threading issue. when the main thread of a process exit, all other threads are terminated (specifically, when any thread calls _exit
, then all threads are terminated, and _start
calls _exit
after main
returns). but your main returns immediately after invoking the other thread. To prevent this, you should call pthread_join
which will wait for a thread to exit before returning. that'll look like this
int a;
void* PrintVar(void* arg){
printf( "%d\n", a);
}
int main(int argc, char*argv[]) {
void *dummy;
a = 10;
pthread_t thr;
pthread_create( &thr, NULL, PrintVar, NULL );
pthread_join( thr, &dummy);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With