- Forward


Shared Variables in Pthreads
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • 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
Back SMYC Forward
  • "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
Back SMYC Forward
  • 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.)
Back SMYC Forward
unixexamples/pthreads/memory0.c
 
Using Global Segments Appropriately
Back SMYC Forward
  • 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.)
Back SMYC Forward
unixexamples/pthreads/memory1.c
 
An Incorrect Use of the Stack
Back SMYC Forward
  • A Question:
    • Can the address of a variable with function scope be stored in a variable with file scope?
    • Yes
      Expand
  • Consequences:
    • What symptoms can arise as a result of using such a pointer?
    • Segmentation faults, garbage, etc...
      Expand
An Incorrect Use of the Stack (cont.)
Back SMYC Forward
unixexamples/pthreads/memory4.c
 
An Inappropriate Use of a Local Static Variable
Back SMYC Forward
  • 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
      Expand
An Inappropriate Use of a Local Static Variable (cont.)
Back SMYC Forward
unixexamples/pthreads/memory3.c
 
A Very Inappropriate Use of the Stack
Back SMYC Forward
  • 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)
      Expand
  • A Question Revisited:
    • Can the address of a variable with function scope be stored in a variable with file scope?
    • Yes
      Expand
A Very Inappropriate Use of the Stack (cont.)
Back SMYC Forward
unixexamples/pthreads/memory2.c
 
A Very, Very Inappropriate Use of the Stack
Back SMYC Forward
  • 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()
      Expand
  • A Question Revisited:
    • Can the address of a variable with function scope be stored in a variable with file scope?
    • Yes
      Expand
A Very, Very Inappropriate Use of the Stack (cont.)
Back SMYC Forward
unixexamples/pthreads/memory6.c
 
Some Important Points
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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):
    • Thread-Specific Data
    • Thread-Local Storage
Might Seem Appropriate But Isn't (cont.)
Back SMYC Forward
unixexamples/pthreads/memory5.c
 
There's Always More to Learn
Back -