Dana Vrajitoru
B424 Parallel and Distributed Programming
The Pthread Library
-
A standardized programming interface for threads programming.
-
Defined in 1995 by IEEE POSIX 1003.1c standard.
-
Implementations which adhere to this standard are referred to as POSIX
threads, or Pthreads.
-
Most hardware vendors now offer Pthreads in addition to their
proprietary API's.
-
Pthreads are defined as a set of C language programming data types and
functions.
-
To use them you need to include the header <pthread.h> and link the
library libpthread with the option -lpthread. The compilation
and linking can be done with gcc and g++.
-
The maximum number of threads that may be created by a process is
implementation dependent.
Basic functions
Creating - terminating threads
Initially, the main() program comprises a single, default
thread. All other threads must be explicitly created by the
programmer, by calling the following function.
int pthread_create (thread, attributes, start_function, argument);
This creates a thread with the specified attributes and executes
the start function within it. The function may have one argument that
must be passed as a void-type pointer (can be a pointer to a complex
data structure). The function returns 0 if the thread was created
successfully.
Once the thread has been created, it has a unique identity that can
be found with the function:
int pthread_self();
There are several ways in which a thread may be terminated:
- The thread returns from its starting routine (the main routine
for the initial thread).
-
The thread itself makes a call to the pthread_exit function.
-
The thread is canceled by another thread via the pthread_cancel
function call.
-
The entire process is terminated due to a call to the exit()
function.
-
If main() finishes before the threads finish their execution, and
exits with pthread_exit(), the other threads will continue to
execute. Otherwise, they will be automatically terminated when main()
finishes.
Thread synchronisation:
int pthread_join (pthread_t thread, void **status);
Forces the parent thread for the one used as argument to wait
until the thread has finished its execution. The value returned by the
thread in the exit function can be found as the second parameter, if
not NULL.
Protecting a critical section: mutex variables.
-
A mutex (mutual exclusion) is a lock that allows the programmer to
control the access to shared resources.
-
All of the shared variables are considered to be critical section and
all access to them should be protected.
-
When several threads try to lock a mutex at the same time, only one
will succeed. The other threads will be blocked until the mutex is
unlocked, and even in this case, only one other thread can lock it
again.
-
For each critical section there should be a different mutex lock to
protect it.
-
The mutex variables have to be shared among the threads accessing the
critical section. In general, they are declared as global.
To create a mutex lock, just declare a variable of the
appropriate type:
pthread_mutex_t mutex;
The lock must then be initialized:
pthread_mutex_init(&mutex, NULL);
Before entering the critical section, each thread should
lock the mutex:
pthread_mutex_lock(&mutex);
When leaving the critical section, the thread must unlock
the mutex:
pthread_mutex_unlock(&mutex);