3

In my textbook it says:

All threads within a process have access to the same data (share)

But each thread has its own stack, it means local variable is not shared. So what kind of data threads can share.

update:

I found each thread can share global variable, it made me confused, what I learn global global variable is static stack, it shouldn't be shared by that each thread has its own stack.

mko
  • 21,334
  • 49
  • 130
  • 191

5 Answers5

2

I found each thread can share global variable, it made me confused, what I learn global global variable is static stack, it shouldn't be shared by that each thread has its own stack.

You're thinking too much about the stack, there are other memory regions such as data or bss. Objects with static storage (such as global variables and those declared with the modifier static) are shared between all threads.

Also, if you try hard, all threads can acces everything, there's nothing special about a different "stack". If a thread manages to get a pointer to a location on another "stack" it can freely read it, modify it etc.

The main point of all this is that threads don't just share variables. They are simply in the same virtual memory space.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
2

Firstly, global and static variables are shared.

Secondly, memory from malloc that you can reach via a pointer in a global variable, (or via a pointer that...) is shared.

Thirdly, memory in one thread's stack that you can reach via a pointer in a global variable, (or via a pointer that...) is shared.

Really, all of it is shared, all the time, but not all of it is reachable. Thread A can access thread B's stack, but it won't have a pointer to do so through unless thread B does something like assign the address of something in its stack to a global (don't do that) or you're doing something where you examine the details of the threads and work your way into their stacks (if you're doing that you're way more knowledgeable enough about the mechanisms of the pthreads implementation than I am*, so I won't tell you not to do that, but as a rule it wouldn't be something to do lightly).

Mostly you only have to worry about global and static, and can consider anything dealing only in locals to be thread-safe, but that gets blown away once you can reach the latter from the former.

*Actually, I know little on that, and am mostly basing this on knowledge of threading in other languages. Really, it's a general threading thing rather than a pthreads specific one, with the exception that some other languages won't even let you fall into the trap of referencing stack memory from global.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
1

@cnicutar is right about the static storage. Actually there is a good discussion about this in another question. Despite the title of that question, the answers there(especially first two) do answer your question well and I don't think I can do better.

Community
  • 1
  • 1
Gnijuohz
  • 3,294
  • 6
  • 32
  • 47
0

As has been said, each thread has its own stack. So data on this stack is said to be thread safe, since only it owns it.

All threads within a process have access to the same data (share)

As it implies, multiple threads have access to same data. This should ring some mini alarm bells since you have to start thinking about thread synchronization (critical sections, mutexes etc) to resources which are shared.

Resources allocated on the heap say through the new operator are shared as all threads have access to the same heap.

Science_Fiction
  • 3,403
  • 23
  • 27
0

I dont think static data will be allocated for each thread. It will be instantiated only once and will be accessible to all the threads having the declaration of that static data in their execute() method..

Yogesh
  • 565
  • 3
  • 21