You are on page 1of 17

pthreads

pthreads
• A POSIX standard (IEEE 1003.1c) API for
thread creation and synchronization
• API specifies behavior of the thread
library. Thread implementation is up to the
development of the library.
• Common in UNIX operating systems
Linux threads
• Linux refers to them as tasks rather than
threads
• Thread creation is done through clone()
system call
• Clone() allows a child task to share the
address space of the parent task (process)
Pthreads:
• pthread_create – to create a thread.
– Arguments:
• 1: pthread_t * - name of the thread
• 2: pthread_attr_t * - attributes of the thread
• 3: void *(*start_routine)(void *) – name of the function
• 4: void * arg – arguments to the function
• Different attributes are
– Detach state
– Scheduling policy
– Schedule param
– Inherit sched
– scope
– Stack size
• pthread_join – called by the main task
• pthread_exit – called by the thread to
return the exit status to the main task.
Scenario
//Main thread //Thread
Pthread_create()
Function()
{
Pthread_join()
Pthread_exit()
}
Thread attributes

• Pthread_attr_init()
• Pthread_attr_set***()
• Pthread_attr_get***()
• Pthread_attr_destroy()
set*** / get***
• ***:
– detachstate()
– schedpolicy()
– schedparam()
– inheritsched()
– scope()
– stacksize()
Example detachstate
// main task // thread
pthread_attr_init() function{
pthread_attr_setdetachstate()
pthread_create()
pthread_attr_destroy() }
Cancelling a thread
• pthread_cancel()
• pthread_setcancelstate()
• pthread_setcanceltype()
– Asynchronous
– Deferred
• Pthread_join(), pthread_cond_wait(),
pthread_cond_timewait(), pthread_testcancel(),
sem_wait(), sigwait() – should be called by the
main task to collect the exit status
usage
// main task // thread
Pthread_create() Function()
{
Pthread_cancel() Pthread_setcancelstate()
Pthread_setcanceltype()
Pthread_join()
/// Jump to exit

Pthread_exit()
}
semaphores
• Binary semaphores
– Mutually exclusive binary semaphores
• Initial value = 1
– Signaling semaphores
• Initial value = 0
• Counting semaphores
– Initial value = n
• Implementation
– Sem_init
– Sem_wait
– Sem_post
– Sem_destroy
Mutex
• Similar to Mutually exclusive binary
semaphore
• Initial value is 1 by default
• Implementation
– Pthread_mutex_init()
– Pthread_mutex_lock()
– Pthread_mutex_unlock()
– Pthread_mutex_destroy()
• Pthread_mutex_init()
– Arguments:
• Mutex name
• Pthread_mutexattr_t – mutex attribute
– PTHREAD_MUTEX_DEFAULT or PTHREAD_MUTEX_NORMAL
– PTHREAD_MUTEX_ERRORCHECK
– PTHREAD_MUTEX_RECURSIVE
Types of mutex
• PTHREAD_MUTEX_DEFAULT or PTHREAD_MUTEX_NORMAL
Results in a deadlock if the same pthread tries to lock it a second time
using the pthread_mutex_lock subroutine without first unlocking it. This
is the default type.

• PTHREAD_MUTEX_ERRORCHECK
Avoids deadlocks by returning a non-zero value if the same thread
attempts to lock the same mutex more than once without first unlocking
the mutex.

• PTHREAD_MUTEX_RECURSIVE
Allows the same pthread to recursively lock the mutex using the
pthread_mutex_lock subroutine without resulting in a deadlock or
getting a non-zero return value from pthread_mutex_lock. The same
pthread has to call the pthread_mutex_unlock subroutine the same
number of times as it called pthread_mutex_lock subroutine in order to
unlock the mutex for other pthreads to use
struct RecursiveLock {
pthread_mutex_t mutex;
pthread_cond_t condition;
unsigned int recursionCount;
pthread_t owner;
};

lock: unlock:
pthread_t thread = lock mutex
pthread_self(); recursionCount--
lock mutex if (!recursionCount) {
if (owner == thread) owner = NULL
recursionCount++; signal condition
else { }
while (recursionCount) unlock mutex
wait on condition
recursionCount = 1
owner = thread
}
unlock mutex
sequence
• Variables:
– Pthread_mutex_t m; name of the mutex
– Pthread_mutexattr_t a ; what type of mutex
• Function calling sequence
– pthread_mutexattr_init (&a);
– Pthread_mutexattr_settype(&a, TYPE ) ;
– Pthread_mutex_init(&m, &a) ;
– Pthread_mutexattr_destroy(&a);

You might also like