- Forward


Thread-Local Storage
in Pthreads


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
  • Shared Variables:
    • Threads can "share" variables in the initialized data, uninitialized data, and heap segments
Overview
Back SMYC Forward
  • Purpose:
    • Allows a function to maintain a separate copy of a variable for each thread that calls the function
  • Comparison to Thread-Specific Storage:
    • Similar but much simpler
    • Not part of the POSIX/SUS standard
Usage
Back SMYC Forward
  • Declaration:
    • Include the __thread specifier immediately after the static or extern specifier (note that there are two underscores)
  • Deallocation:
    • The memory is freed by the system when the thread terminates
An Example Using Thread-Local Storage
Back SMYC Forward
unixexamples/threadspecificdata/grader_threadlocal.c
 
There's Always More to Learn
Back -