Shared Variables in Pthreads
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Review
-
Threads
- A mechanism that permits a single process to perform
multiple tasks concurrently
- Distinct Attributes:
- Thread ID
-
errno
- Stack (in the stack space of the process)
- Shared Attributes:
- Process, group and session IDs
- Open file descriptors
- Signal dispositions
- Text/code segment
- Initialized data, uninitialized data, and heap segments
Implications for Variable Sharing
- "Global" Data Segments:
- Threads can "share" variables in the initialized data,
uninitialized data, and heap segments
- Stack:
- Threads shouldn't "share" variables on the stack (but can,
in some sense, because each thread's stack is in the stack
space of the process)
Using Function/Block Scope Appropriately
- Single Threaded Programs:
- Function/block scope means the same identifier
in different functions/blocks refers to different entities
- Multi-Threaded Programs:
- Distinct stacks means the same identifier in the same
function/block in different threads refers to
different entities
Using Function/Block Scope Appropriately (cont.)
unixexamples/pthreads/memory0.c
Using Global Segments Appropriately
- Single Threaded Programs:
- File scope means one identifier can be used in different
functions to refer to the same entity
- Multi-Threaded Programs:
- File scope means one identifier can be used in different
threads to refer to the same entity
Using Global Segments Appropriately (cont.)
unixexamples/pthreads/memory1.c
An Incorrect Use of the Stack
- A Question:
- Can the address of a variable with function scope
be stored in a variable with file scope?
-
Yes
- Consequences:
- What symptoms can arise as a result of using such a
pointer?
-
Segmentation faults, garbage,
etc...
An Incorrect Use of the Stack (cont.)
unixexamples/pthreads/memory4.c
An Inappropriate Use of a Local Static Variable
- An Observation:
- Variables can be declared/defined in a function
and given static duration (even though they have function scope)
- Recall:
- Such variables are in the unitialized data segment (BSS)
- A Question Revisited:
- Can the address of a variable with function scope
be stored in a variable with file scope?
-
Yes
An Inappropriate Use of a Local Static Variable (cont.)
unixexamples/pthreads/memory3.c
A Very Inappropriate Use of the Stack
- Another Question:
- What is the duration of a variable with function
scope that is declared/defined in
main()
?
-
It has automatic duration but
it is almost as long as a variable with static
duration (because main()
is the first function
called and doesn't exit until termination)
- A Question Revisited:
- Can the address of a variable with function scope
be stored in a variable with file scope?
-
Yes
A Very Inappropriate Use of the Stack (cont.)
unixexamples/pthreads/memory2.c
A Very, Very Inappropriate Use of the Stack
- Still Another Question:
- What variables are on the stack when
main()
calls
foo()
which itself calls bar()
?
-
The variables declared/defined in
bar()
on top of those declared/defined in
foo()
on top of those declared/defined in
in main()
- A Question Revisited:
- Can the address of a variable with function scope
be stored in a variable with file scope?
-
Yes
A Very, Very Inappropriate Use of the Stack (cont.)
unixexamples/pthreads/memory6.c
Some Important Points
- Single-Threaded Programs:
- Using pointers (with file scope) to variables with
function scope is inappropriate in single-threaded
programs (but you are unlikely to want to do it)
- Multi-Threaded Programs:
- In multi-threaded programs it's something that you're
more likely to want to do but should avoid
Might Seem Appropriate But Isn't
- Being "Clever":
- Declare/define a variable with function scope and static
duration in the entry-point for the thread and
then assign its address to a pointer with file scope
- The Rationale:
- They're "owned by" the thread
- The Problems:
- The order in which the threads are created might matter
- The Right Way(s):
Might Seem Appropriate But Isn't (cont.)
unixexamples/pthreads/memory5.c
There's Always More to Learn