2

when I create two threads with the same function, the static variables in the function is shared ? and where are they saved ? because if they are saved in static data of the main thread they will be shared for both threads.

code example :

void fill_g_msg( void ){
    pthread_t t1, t2;
    pthread_create(&t1, NULL, thread_fill, “first");
    pthread_create(&t2, NULL, thread_fill, “second");
    // wait for both threads to finish
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);
}


void* thread_fill(void *arg){
    int a;
    if( strcmp((char*)arg, “second”) == 0 ) {
        a = 1;
    } else {
       a = 2;
    }
    return null;
}
Moawiya
  • 147
  • 1
  • 2
  • 11

2 Answers2

1

Each of the threads will be running a different 'instance' of the function, so no, they will not be using exactly the same variable 'a'.

EDIT AGAIN: If you are declaring int a in the function the thread is running, then each thread will have a different copy of it. Each thread has its own stack, and that is where it should go.

Also, I'm not sure you understand what you mean when you say static variable (maybe you meant static local) . Check out this post.

Community
  • 1
  • 1
Matt M
  • 86
  • 9
  • so you mean that the function will be saved in the thread stack ? as I know the static variables of any code is saved in static data that is shared for all threads, and in our case the two threads I create with the main thread is sharing the static data ,so "a" must be in static data right ? because it's static variable , am I right ? – Moawiya Jan 17 '15 at 17:55
  • 1
    Look at the hyperlink above, I think you are confusing static variables and local variables. – Matt M Jan 17 '15 at 17:57
1

I'm new to multithreading too and I found your question very interesting. After searching around for a bit I found the following:

I understand that my references may not be the best (...wikipedia) to refer to but they do have some information that you might find useful.

Quoting wikipedia: "Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.This is sometimes needed because normally all threads in a process share the same address space, which is sometimes undesirable. In other words, data in a static or global variable is normally always located at the same memory location, when referred to by threads from the same process. Variables on the call stack however are local to threads, because each thread has its own stack, residing in a different memory location."

So it says that normally threads share static variables. So I assume the static variables are located exactly at the same place where static variables are located in processes that don't have more than one threads. I may be wrong of course :-)

In addition, if you look at my GCC reference below though, you might find that you can have static variables that are thread specific with TLS.

I hope that answer was of some use to you.

Best of luck!

References: Wikipedia and GCC

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Serapheim
  • 66
  • 6