- Forward


Thread-Specific Data
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
  • What a Function Requires of the Library:
    • The ability to allocate a block of memory the first time a thread calls the function
    • The ability to obtain the address of the memory without maintaining a pointer in an automatic variable
    • The ability to free the memory when the thread terminates (which may happen in any function)
Create a Key: pthread_key_create() pthread_key_create
Back SMYC Forward
int pthread_key_create(pthread_key_t *key, void (*destructor)(void *)
Purpose:
Create a thread-specific key (and install a destructor)
Details:
key A key that can be used to obtain thread-specific data
destructor The function to call to free the thread-specific memory (or NULL if no destructor is needed)
Return 0 on success; a positive error number on error
#include <pthread.h> pthread.h

Note: destructor() will be invoked automatically by the system when the thread terminates (and passed the address of the thread-specific memory for this key).

Set a Value: pthread_setspecific() pthread_setspecific
Back SMYC Forward
int pthread_setspecific(pthread_key_t key, const void *value)
Purpose:
Save a value in thread-specific memory
Details:
key The key used to identify the thread-specific data
value The value to save (which is normally a block of memory that has been previously allocated by the caller)
Return 0 on success; a positive error number on error
#include <pthread.h> pthread.h
Get a Value: pthread_getspecific() pthread_getspecific
Back SMYC Forward
void *pthread_getspecific(pthread_key_t key)
Purpose:
Retrieve a value from thread-specific memory
Details:
key The key used to identify the thread-specific data
Return A pointer to the value on success; NULL is there is no thread-specific data associated with the key
#include <pthread.h> pthread.h
A Very, Very Problematic Motivating Example
Back SMYC Forward

A Driver with a Single Thread

unixexamples/threadspecificdata/driver.c
 
A Very, Very Problematic Motivating Example (cont.)
Back SMYC Forward
unixexamples/threadspecificdata/grader_local.c
 
A Very Problematic Motivating Example
Back SMYC Forward

A Driver with Multiple Threads

unixexamples/threadspecificdata/threaded_driver.c
 
A Very Problematic Motivating Example (cont.)
Back SMYC Forward
unixexamples/threadspecificdata/grader_static.c
 
A Problematic Motivating Example
Back SMYC Forward
unixexamples/threadspecificdata/grader_file.c
 
An Example Using Thread-Specific Data
Back SMYC Forward
unixexamples/threadspecificdata/grader_specific.c
 
There's Always More to Learn
Back -