L11: POSIX Threads Flashcards
What are Threads?
- Lightweight Processes
- Only local variables in a function are specific to a thread
- Each thread has it’s own stack
- Most other data is shared between threads
- global variables
- the heap
Posix Threads:
- Standard
- Library
Standard:
pthreads
Library:
pthread.h
Reasons to
use Threads
- Efficiency
- Ease of data sharing
- Leverages parallelism
Common uses of
Parallelism
- Overlapping I/O
- Can perform long I/O and CPU tasks at the same time
- Asynchronous Events
- Wait for a response AND do something else at the same time
- Real-Time Scheduling
- Quickly respond to important tasks
- Utilize Multiple Processors
Threads
vs
Processes
- Threads use less system resources for the same task
- But, thread may require more user space, depending on the implementation
- Threads within the same process share everything but the stack and processor state
- Many process-based mechanisms have thread based equivalents that can be used:
- non-blocking I/O
- Shared Memory (IPC)
- signals
- jmp() function
- Many process-based mechanisms have thread based equivalents that can be used:
- Threads use an inherently simpler shared memory mechanism than processes
- Interthread communication is far easier
- Much easier to code for parallelism errors, such as race conditions
What is
POSIX?
Portable Operating System Interface (added X for style)
- A family of standards specified by the IEEE Computer Society
- Helps maintain compatibility between operating systems
- Defines :
- API
- Command Line Shells
- Utility Interfaces
- For software compatible with varients of Unix and other Operating Systems
Problems
with
Threads
- Unpredictable functioning( non-determinism)
- Difficult to Debug
- Impossible to prove correct
- Almost impossible to test thoroughly
- Specific behavior not easily reproducible
- Simple conceptually, but added complexities for dealing with many pitfalls
pthread Library:
Function for
Creating a Thread
pthread_create()
Full Declaration:
int pthread_create( p_thread_t *thread,
const pthread_attr_t attr,
void *(*func)(void *),
void *arg);
- Creates a new thread with the attributes specified in attr
- attr can be NULL
- Starts executing func() and passes it arg
- Consider carefully what to pass as argument
- Return 0 if ok, nonzero if error
pthread Libary:
Function to
Rejoin Threads
pthread_join()
Full Declaration:
int pthread_join( pthread_t thread,
void **value_ptr);
- Makes the calling thread wait for the specified thread to terminate
- value_ptr is assigned its return value, or assigned PTHREAD_CANCELLED
Important Parts
of
Thread Synchronization
- pthread_join()
- Similar to wait() for processes
- mutex variables
- Similar to binary semaphores for processes
- Condition Variables
- wait for an “event”
- A variable is assigned a certain value, which is “signaled” by another thread
pthread Library:
Important Structures
pthread_t
pthread_mutex_t
pthread_attr_t
pthread Library:
Important Functions
- Thread related
- pthread_create()
- pthread_join()
- Mutex related
- pthread_mutex_init()
- pthread_mutex_lock( pthread_mutex_t *mutex)
- pthread_mutex_trylock( pthread_mutex_t *mutex)
- pthread_mutex_unlock( pthread_mutex_t *mutex)
Threads and Process Management:
What does an “exec()” call from a thread
do to the threads in the
containing process?
All the threads will terminate.
A new thread is created for the
program to be executed.
Posix Threads:
Topics (6)
- Background
- Threads vs Processes
- Thread Synchronization
- Mutex Variables
- Condition Variables
- Threads and Unix
What if a thread
blocks
inside a library function?
The pthreads library includes many functions which
only block the thread, not the entire process.
The programmer can also turn off blocking in other function.