Linux Tutorial - POSIX Threads
Linux Tutorial - POSIX Threads
html
Table of Contents:
# Thread Basics
# Thread Creation and Termination
# Thread Synchronization
# Thread Scheduling
# Thread Pitfalls
# Thread Debugging
# Thread Man Pages
# Links
# Books
Thread Basics:
Related YoLinux
Tutorials:
Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data
° C++ on Linux management and process interaction.
A thread does not maintain a list of created threads, nor does it know the thread that created it.
° C++ STL (Standard All threads within a process share the same address space.
Template Library) example Threads in the same process share:
of a linked list using a list Process instructions
Most data
° C++ string class
examples
open files (descriptors)
signals and signal handlers
° X-emacs and C++ current working directory
development User and group id
Each thread has a unique:
° C++ Structure Example Thread ID
and Tutorial set of registers, stack pointer
stack for local variables, return addresses
° Linux software
development tutorial
signal mask
priority
°YoLinux Tutorials Index Return value: errno
pthread functions return "0" if OK.
Example: pthread1.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
1 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Compile:
Run: ./a.out
Results:
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
Details:
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.
Ads by Google
Linux Unix
Linux Program
Function call: pthread_create
Linux Downloads
Linux Server int pthread_create(pthread_t * thread,
RedHat Linux const pthread_attr_t * attr,
void * (*start_routine)(void *),
void *arg);
Free Information
Technology Magazines Arguments:
and Document thread - returns the thread id. (unsigned long int defined in bits/pthreadtypes.h)
Downloads 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)
scheduling policy (real-time? PTHREAD_INHERIT_SCHED,PTHREAD_EXPLICIT_SCHED,SCHED_OTHER)
scheduling parameter
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)
Free Information stack size (default minimum PTHREAD_STACK_SIZE set in pthread.h),
Technology Software void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to
and Development void.
Magazine *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
Subscriptions and
Document Downloads Function call: pthread_exit
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:
void print_message_function( void *ptr );
2 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
...
...
iret1 = pthread_create( &thread1, NULL, (void*)&print_message_function, (void*) message1);
...
...
Thread Synchronization:
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 operations by multiple threads upon the same memory area
performed at the same time or to prevent race conditions where an order of operation upon the memory is expected. A
contention or 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 such as memory. 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.
D-Link
Possible execution sequence
DWL-2100AP
SNMP AES 802.11g Thread 1 Thread 2 Thread 1 Thread 2
1...
D-Link Systems,
counter = 0 counter = 0 counter = 0 counter = 0
In...
Thread 2 locked out.
New $96.99 counter = 1 counter = 1 counter = 1
Macally Portable Thread 1 has exclusive use of variable counter
Goose Neck USB
Vide... counter = 2
Macally
New $24.79
If register load and store operations for the incrementing of variable counter occurs with unfortunate timing, it is
HP 57 Tri-Color theoretically possible to have each thread increment and overwrite the same variable with the same value. Another
Inkjet Print Cartrid...
Hewlett Packard
possibility is that thread two would first increment counter locking out thread one until complete and then thread one
New $25.75 would increment it to 2.
D-Link DUB-H4
Sequence Thread 1 Thread 2
High Speed USB 1 counter = 0 counter=0
2.0 4-P...
D-Link Thread 1 locked out.
New $19.99 2 counter = 1
Thread 2 has exclusive use of variable counter
D-Link DUB-H7 3 counter = 2
High Speed USB
2.0 7-P...
D-Link Systems, Code listing: mutex1.c
In...
New $25.22
void *functionC();
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
Privacy Information int counter = 0;
main()
{
int rc1, rc2;
pthread_t thread1, thread2;
3 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
exit(0);
}
void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
printf("Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}
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 waits for the completion of the threads with a join.
#include <stdio.h>
#include <pthread.h>
#define NTHREADS 10
void *thread_function(void *);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
main()
{
pthread_t thread_id[NTHREADS];
int i, j;
/* Now that all threads are complete I can print the final result. */
/* Without the join I could be printing a value before all the threads */
/* have been completed. */
4 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Results:
Thread number 1026
Thread number 2051
Thread number 3076
Thread number 4101
Thread number 5126
Thread number 6151
Thread number 7176
Thread number 8201
Thread number 9226
Thread number 10251
Final counter value: 10
Condition Variables:
A condition variable is a variable of type pthread_cond_t and is used with the appropriate functions for waiting and later,
process continuation. The condition variable mechanism allows threads to suspend execution and relinquish the processor
until some condition is true. A condition variable must always be associated with a mutex to avoid a race condition created
by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on
it resulting in a deadlock. The thread will be perpetually waiting for a signal that is never sent. Any mutex can be used,
there is no explicit link between the mutex and the condition variable.
Creating/Destroying:
pthread_cond_init
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_cond_destroy
Waiting on condition:
pthread_cond_wait
pthread_cond_timedwait - place limit on how long it will block.
Waking thread based on condition:
pthread_cond_signal
pthread_cond_broadcast - wake up all threads blocked by the specified condition variable.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *functionCount1();
void *functionCount2();
int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
main()
{
pthread_t thread1, thread2;
exit(0);
}
void *functionCount1()
{
for(;;)
{
pthread_mutex_lock( &condition_mutex );
while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
{
pthread_cond_wait( &condition_cond, &condition_mutex );
}
pthread_mutex_unlock( &condition_mutex );
pthread_mutex_lock( &count_mutex );
count++;
printf("Counter value functionCount1: %d\n",count);
pthread_mutex_unlock( &count_mutex );
void *functionCount2()
{
5 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
for(;;)
{
pthread_mutex_lock( &condition_mutex );
if( count < COUNT_HALT1 || count > COUNT_HALT2 )
{
pthread_cond_signal( &condition_cond );
}
pthread_mutex_unlock( &condition_mutex );
pthread_mutex_lock( &count_mutex );
count++;
printf("Counter value functionCount2: %d\n",count);
pthread_mutex_unlock( &count_mutex );
Note that functionCount1() was halted while count was between the values COUNT_HALT1 and COUNT_HALT2. The
only thing that has been ensures is that functionCount2 will increment the count between the values COUNT_HALT1 and
COUNT_HALT2. Everything else is random.
The logic conditions (the "if" and "while" statements) must be chosen to insure that the "signal" is executed if the "wait" is
ever processed. Poor software logic can also lead to a deadlock condition.
Note: Race conditions abound with this example because count is used as the condition and can't be locked in the while
statement without causing deadlock. I'll work on a cleaner example but it is an example of a condition variable.
Thread Scheduling:
When this option is enabled, each thread may have its own scheduling properties. Scheduling attributes may be specified:
The threads library provides default values that are sufficient for most cases.
Thread Pitfalls:
Race conditions: While the code may appear on the screen in the order you wish the code to execute, threads are
scheduled by the operating system and are executed at random. It cannot be assumed that threads are executed in
the order they are created. They may also execute at different speeds. When threads are executing (racing to
complete) they may give unexpected results (race condition). Mutexes and joins must be utilized to achieve a
predictable execution order and outcome.
Thread safe code: The threaded routines must call functions which are "thread safe". This means that there are no
static or global variables which other threads may clobber or read assuming single threaded operation. If static or
global variables are used then mutexes must be applied or the functions must be re-written to avoid the use of these
variables. In C, local variables are dynamically allocated on the stack. Therefore, any function that does not use
static data or other shared resources is thread-safe. Thread-unsafe functions may be used by only one thread at a
time in a program and the uniqueness of the thread must be ensured. Many non-reentrant functions return a pointer
to static data. This can be avoided by returning dynamically allocated data or using caller-provided storage. An
example of a non-thread safe function is strtok which is also not re-entrant. The "thread safe" version is the
re-entrant version strtok_r.
Mutex Deadlock: This condition occurs when a mutex is applied but then not "unlocked". This causes program
execution to halt indefinitely. It can also be caused by poor application of mutexes or joins. Be careful when applying
two or more mutexes to a section of code. If the first pthread_mutex_lock is applied and the second
pthread_mutex_lock fails due to another thread applying a mutex, the first mutex may eventually lock all other
threads from accessing data including the thread which holds the second mutex. The threads may wait indefinitely
for the resource to become free causing a deadlock. It is best to test and if failure occurs, free the resources and
stall before retrying.
6 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
...
pthread_mutex_lock(&mutex_1);
while ( pthread_mutex_trylock(&mutex_2) ) /* Test if already locked */
{
pthread_mutex_unlock(&mutex_1); /* Free resource to avoid deadlock */
...
/* stall here */
...
pthread_mutex_lock(&mutex_1);
}
count++;
pthread_mutex_unlock(&mutex_1);
pthread_mutex_unlock(&mutex_2);
...
The order of applying the mutex is also important. The following code segment illustrates a potential for deadlock:
void *function1()
{
...
pthread_mutex_lock(&lock1); - Execution step 1
pthread_mutex_lock(&lock2); - Execution step 3 DEADLOCK!!!
...
...
pthread_mutex_lock(&lock2);
pthread_mutex_lock(&lock1);
...
}
void *function2()
{
...
pthread_mutex_lock(&lock2); - Execution step 2
pthread_mutex_lock(&lock1);
...
...
pthread_mutex_lock(&lock1);
pthread_mutex_lock(&lock2);
...
}
main()
{
...
pthread_create(&thread1, NULL, function1, NULL);
pthread_create(&thread2, NULL, function1, NULL);
...
}
If function1 acquires the first mutex and function2 acquires the second, all resources are tied up and locked.
Condition Variable Deadlock: The logic conditions (the "if" and "while" statements) must be chosen to insure that the
"signal" is executed if the "wait" is ever processed.
Thread Debugging:
GDB:
Debugging Programs with Multiple Threads
GDB: Stopping and starting multi-thread programs
GDB/MI: Threads commands
DDD:
Examining Threads
7 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
Links:
News Groups:
8 of 9 03/10/2010 10:55
Linux Tutorial: POSIX Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
comp.programming.threads
comp.unix.solaris
Books:
9 of 9 03/10/2010 10:55