Re: POSIX thread question



On 28 Aug 2006 14:15:21 -0700, Jack <junw2000@xxxxxxxxx> wrote:
Below is simple code of POSIX thread from a book:
[snip]
It creates 10 threads. My questions are:
1. The 10 threads share the same code. Do the 10 threads share both
main() and myThread(), or just myThread()?

Everything, though if myThread() exits, it will not return to main()
but terminate the thread. myThread _can_ for example call main() if
it likes, though...

2. The 10 threads share the same variables. Do the 10 threads share all
the variables in the code? or only global variables?

Depends on what you mean by "share". They share all the process memory,
so they will be _able_ to access all variables (which I would hope would
most often be a bug), though the symbols used for stack-allocated
variables will refer to the thread's own copy in the running code.

3. The variable protVariable (LINE1) is shared among the 10 threads.
All the 10 threads can modify protVariable, right?

Yes. Hence the need to protect the variable use with mutices, otherwise
you might end up with inconsistent results.

4. I use the following command to compile it:
gcc -o pt pthread.c

You need the -pthread switch to compile threaded code with gcc.

--
Mikko Rauhala - mjr@xxxxxx - <URL:http://www.iki.fi/mjr/>
Transhumanist - WTA member - <URL:http://www.transhumanism.org/>
Singularitarian - SIAI supporter - <URL:http://www.singinst.org/>

.