Pthreads Flashcards
What is a Mutex
Mutual Exclusion. Used to guarantee that one thread
“excludes” all other threads while it executes the critical section.
Issues with using Mutexes
Busy-waiting enforces the order threads access a critical section.
Using mutexes, the order is left to chance and the system.
There are applications where we need to control the order threads access the critical section.
What is a Sephamore?
A semaphore limits the number of simultaneous accesses allowed by incrementing and decrementing a counter with the wait (decrement) and signal (increment)
Semaphores are more powerful than mutexes since they can be initialized to any nonnegative value.
What is a Barrier?
A barrier is a point in a program at which the threads block until all of the threads have reached it.
Condition Variables
A condition variable is a data object that allows a thread to suspend execution until a certain event or condition occurs.
When the event or condition occurs another thread can signal the thread to “wake up.”
A condition variable is always associated with a mutex.
Processes and Threads
In Pthreads a process is the running instance of the program and a thread is a light weight process ( A sequence of statements within the program)
Starting threads
In Pthreads, the threads are started by the executable
Unlike in MPI where they are usually started by a script
Stopping the threads
We call the function pthread_join once for each thread.
A single call to pthread_join will wait for the thread associated with the pthread_t object to complete.
Matrix multiplication
Pthreads can perform matrix multiplication without critical sections
Read/Write lock
A read-write lock is used when it’s safe for multiple threads to simultaneously read a data structure, but if a thread needs to modify or write to the data structure, then only that thread can access the data structure during the modification.
Thread Safety
A block of code is thread-safe if it can be
simultaneously executed by multiple
threads without causing problems.
Some C functions cache data between
calls by declaring variables to be static,
causing errors when multiple threads call
the function.
Thread Unsafe Libraries
strtok
random
localtime
Shared vs Distributed Programming
A thread in shared-memory programming is analogous to a process in distributed memory programming.
However, a thread is often lighter-weight
than a full-fledged process.
Shared vs Private Variables
In Pthreads programs, all the threads have
access to global variables, while local variables usually are private to the thread running the function.
What is a Race Condition
When indeterminacy results from multiple threads attempting to access a shared resource such as a shared variable or a shared file, at least one of the accesses is an update, and the accesses can result in an error, we have a race condition.