Title: Are variables declared in pthread's functions shared between threads?
Tags: c;multithreading;pthreads
Question: I am new to C and pthreads. Here's an example (which probably won't compile as I wrote it now) but it shows what I want to know:

int i = 50;

```void* myFunc (void* whatever) {
    int myLoop = 0;
    for (;;) 
    {
        for (myLoop = 0; myLoop < i; myLoop++)
            do work blah;
    }
    pthread_exit(NULL);
}

int main (void) {
    pthread_t threadNo1, threadNo2, threadNo3;

    pthread_create(&amp;threadNo1, NULL, myFunc, NULL);
    pthread_create(&amp;threadNo2, NULL, myFunc, NULL);
    pthread_create(&amp;threadNo3, NULL, myFunc, NULL);

    pthread_join(threadNo1, NULL);
    pthread_join(threadNo2, NULL);
    pthread_join(threadNo3, NULL);

    pthread_exit(NULL);
}
```

Is ```int myLoop``` shared between the 3 threads? 

Is the loop in ```myFunc``` running as thought on all 3 threads (as in iterating properly to 50 which is the value of ```int i```), or are they fighting continuously over its value due to ```myLoop++```? Is this because the variable ```int myLoop``` shares its memory value in all different threads?

I want myFunc to run on different threads given to different cores without them messing with each other while looping, meaning the loop iterates perfectly on all threads and they are ignorant of each other. Or is this not actually a problem and is indeed doing that?

Comment: Each thread has its own stack.

Comment: It serves the purpose of deadlocking your program. Had the main thread just exited, the runtime would kill all other threads.

Comment: @StoryTeller while the join() call o'death does indeed prevent main() from exiting, it is not deadlock.  The pthreads are not waiting on a resource held by the main thread - they are, (presumably:), making forward progress with their 'do work blah'.

Comment: A local variable is a local variable. No sharing will happen.

Comment: Thank you. I also assume the `pthread_join` I added serves no purpose since the functions never end?

Here is the accepted answer: ```
  Is int myLoop shared between the 3 threads?
```

Threads have nothing to do with it. The same question could be asked about a single thread. Consider:

```int myFunc(int *j)
{
    int q;
    // lots of code here
    myFunc (&amp;q);
}
```

Consider a single thread that calls ```myFunc``` and then ```myFunc``` calls itself. Do the two instances of ```myFunc``` share ```q``` or not? And the answer is -- both. Each invocation has its own ```q```. But if the second call does ```*j = 2;``` it will change the value of the first invocation's ```q```.

So each has its own, but they can share them if they wish since they have the same view of process memory.

Same with threads.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:12/07/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><multithreading><pthreads>', 'Title': "Are variables declared in pthread's functions shared between threads?", 'ViewCount': 419, 'id': 47689169, 'user_AboutMe': '', 'user_AccountId': 12038955.0, 'user_DisplayName': 'RSS', 'user_Reputation': 123.0, 'user_Views': 15.0, 'user_id': 8806557.0}}