Re: Data sharing in POSIX thread?




David Schwartz wrote:
Jack wrote:

My book says all the threads of the same process share the same data.
In the above code, 10 threads are created. Do they share all the
variables? For the global variable, protVariable, the 10 threads should
share it, it is understandable. How about the local variables i and ret
in
myThread()? Does each thread have its own copy of local variables or do
all the threads share the local variables?

Both. Each thread has its own copy of the local variables and each of
those are shared.

For example, if thread 1 and thread 2 each print out the address of the
"same" local variable, they will print different addresses. If they
somehow communicate those addresses to each other, each can access the
other's variable (assuming they are careful to do so during its
lifetime!).

If I allocate memory in
myThread() using malloc as LINE2, do all the threads share the same
allocated memory? I can not find the answers from my book.

If each thread calls 'malloc', then each thread allocates memory. Those
blocks of memory are fully shared and nothing stops one thread from
accessing the memory another thread allocated.
Does it means that if a thread allocate memory the thread must protect
the allocated memory. Otherwise all threads can access it and the
result is unpredictable.


This is a *very* important concept to understand. Questions like "does
each thread have its own stack or are the stacks shared" try to
pigeon-hole into two possibilities and ignore the third possibility --
each thread does have its own stack (that is, there is one stack per
thread) but they are also shared (that is, each thread can access
another thread's stack should it be coded to do so or do so through a
bug).
How to tell whether a stack is shared or not?

Thanks.

.



Relevant Pages

  • Re: Data sharing in POSIX thread?
    ... Does each thread have its own copy of local variables or do ... If each thread calls 'malloc', ... each thread does have its own stack (that is, ...
    (comp.os.linux.development.system)
  • Re: C garbage collection -crazy idea
    ... node allocation function in my binary tree builder grabbed. ... then call those functions lmalloc... ... You can assign locally allocated memory to local ... similar (but only on the stack). ...
    (comp.lang.c)
  • Re: Local variables controversial?
    ... >>>Calforth uses local variables, but doesn't set a policy of what's good ... they are implemented using a stack frame. ... is still on the Forth data stack? ...
    (comp.lang.forth)
  • Re: Local variables controversial?
    ... Calforth uses local variables, but doesn't set a policy of what's good ... of simply popping the old stack pointer off the stack frame and ... Do I understand you right to mean that the actual data in the stack frame is still on the Forth data stack? ...
    (comp.lang.forth)
  • Re: MM Benchmarks oddities
    ... > From your previous posts I was under the impression this one ... > I kinda assumed that you were planning to test record alignment ... > the allocated memory isn't directly used to place the values, ... The result is returned at the stack and a pointer ...
    (borland.public.delphi.language.basm)