The POSIX thread libraries are a standards based thread API for C/C++.
It allows one to spawn a new concurrent process flow. It is most effective
on multi-processor or multi-core systems where the process flow can be scheduled to run on
another processor thus gaining speed through parallel or distributed processing.
Threads require less overhead than "forking" or spawning a new process because
the system does not initialize a new system virtual memory space and environment for
the process. While most effective on a multiprocessor system, gains are
also found on uniprocessor systems which exploit latency in I/O and other
system functions which may halt process execution. (One thread may execute
while another is waiting for I/O or some other system latency.)
Parallel programming technologies such as MPI and PVM are used in a distributed
computing environment while threads are limited to a single computer system.
All threads within a process share the same address space.
A thread is spawned by defining a function and it's arguments which will
be processed in the thread.
The purpose of using the POSIX thread library in your software is
to execute software faster.
/* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */
In this example the same function is used in each thread. The arguments
are different. The functions need not be the same.
Threads terminate by explicitly calling pthread_exit,
by letting the function return, or by a call to the function exit
which will terminate the process including any threads.
thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)
attr - Set to NULL if default thread attributes are used.
(else define members of the struct pthread_attr_t defined in
bits/pthreadtypes.h)
Attributes include:
detached state (joinable? Default: PTHREAD_CREATE_JOINABLE. Other option: PTHREAD_CREATE_DETACHED)
inheritsched attribute (Default: PTHREAD_EXPLICIT_SCHED Inherit from parent thread: PTHREAD_INHERIT_SCHED)
scope (Kernel threads: PTHREAD_SCOPE_SYSTEM User threads: PTHREAD_SCOPE_PROCESS Pick one or the other not both.)
guard size
stack address (See unistd.h and bits/posix_opt.h _POSIX_THREAD_ATTR_STACKADDR)
stack size (default minimum PTHREAD_STACK_SIZE
set in pthread.h),
void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void.
*arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
Function call:
void pthread_exit(void *retval);
Arguments:
retval - Return value of thread.
This routine kills the thread. The pthread_exit function never
returns. If the thread is not detached, the thread id and return value
may be examined from another thread by using pthread_join.
Note: the return pointer *retval, must not be of local scope
otherwise it would cease to exist once the thread terminates.
[C++ pitfalls]: The above sample program
will compile with the GNU C and C++ compiler g++.
The following function pointer representation below
will work for C but not C++. Note the subtle differences and avoid the
pitfall below:
The threads library provides three synchronization mechanisms:
mutexes - Mutual exclusion lock: Block access to variables
by other threads. This enforces exclusive access by a thread to a
variable or set of variables.
joins - Make a thread wait till others are complete (terminated).
condition variables - data type pthread_cond_t
Mutexes:
Mutexes are used to prevent data inconsistencies due to race conditions.
A race condition often occurs when two or more threads need to perform
operations on the same memory area, but the results of computations depends on
the order in which these operations are performed. Mutexes are used for
serializing shared resources. Anytime a global resource is accessed by more
than one thread the resource should have a Mutex associated with it.
One can apply a mutex to protect a segment of memory ("critical region") from other threads.
Mutexes can be applied only to threads in a single process and do not work
between processes as do semaphores.
Example threaded function:
Without Mutex
With Mutex
int counter=0;
/* Function C */ void functionC() {
counter++
}
/* Note scope of variable and mutex are the same */ pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter=0;
/* Function C */ void functionC() { pthread_mutex_lock( &mutex1 ); counter++ pthread_mutex_unlock( &mutex1 ); }
Possible execution sequence
Thread 1
Thread 2
Thread 1
Thread 2
counter = 0
counter = 0
counter = 0
counter = 0
counter = 1
counter = 1
counter = 1
Thread 2 locked out.
Thread 1 has exclusive use of variable counter
counter = 2
If register load and store operations for the incrementing of variable counter
occurs with unfortunate timing, it is theoretically possible to have each
thread increment and overwrite the same variable with the same value.
Another possibility is that thread two would first increment counter
locking out thread one until complete and then thread one would increment it to 2.
Sequence
Thread 1
Thread 2
1
counter = 0
counter=0
2
Thread 1 locked out.
Thread 2 has exclusive use of variable counter
counter = 1
3
counter = 2
Code listing: mutex1.c
#include #include #include
void *functionC(); pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; int counter = 0;
main() { int rc1, rc2; pthread_t thread1, thread2;
/* Create independent threads each of which will execute functionC */
/* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */
Compile: cc -lpthread mutex1.c
Run: ./a.out
Results:
Counter value: 1 Counter value: 2
When a mutex lock is attempted against a mutex which is held by another thread,
the thread is blocked until the mutex is unlocked.
When a thread terminates, the mutex does not unless explicitly unlocked.
Nothing happens by default.
Joins:
A join is performed when one wants to wait for a thread to finish. A thread
calling routine may launch multiple threads then wait for them to finish
to get the results. One wait for the completion of the threads with a join.