Title: Lecture 25 POSIX Threads Again
1Lecture 25POSIX Threads Again
CSCE 510 Systems Programming
- Topics
- Threads
- Pthread Library
- Dec Pthreads primes example again
November 22, 2005
2- Last Time
- Test 2
- POSIX
- Threads Overview
- Today
- Pthreads
- Pthread Library
- ftp//ftp.uu.net/published/oreilly/nutshell/pthrea
ds/examples.tar.gz - http//www.pdc.kth.se/training/Talks/SMP/maui98/so
nnad/threads_only/talk/lecture.html - Previews
- Thursday eat and write systems programs
- November 29, December 1 last classes
- December 3, Sat. Reading day
- Final Thursday, December 8 - 530 p.m
3Threads Overview Again
4Creating Threads
- include ltpthread.hgt
- int pthread_create(pthread_t thread,
pthread_attr_t attr, void (start_routine)(void
), void arg) - pthread_t thread // the thread pointer
- pthread_attr_t attr // attributes for thread to
be created - void (start_routine)(void ) // thread
executes this - void arg // passing this arg
- When the thread is created the routine
start_routine is called with argument arg.
5Ending Threads
- int pthread_join(pthread_t thread, void
status) - suspends the execution of the calling thread
until the thread identified by Thread
terminates - Thread the thread to
- Status pointer to return status of the
terminating thread - void pthread_exit(void status)
- Call pthread_exit to terminate threads including
main - int pthread_detach()
- put the this thread in the detached state until
it terminates
6Mutexes Mutual Exclusion Objects
- Man pthread_mutex_init
- A mutex is a MUTual EXclusion device, and is
useful for protecting shared data structures from
concurrent modifications, and implementing
critical sections and monitors. - A mutex has two possible states
- unlocked (not owned by any thread), and
- locked (owned by one thread).
- A mutex can never be owned by two different
threads simultaneously. - A thread attempting to lock a mutex that is
already locked by another thread is suspended
until the owning thread unlocks the mutex first. - So whats a mutex? Binary semaphore?
7Mutex Manipulating Functions
- int pthread_mutex_init(pthread_mutex_t mutex,
const pthread_mutex-attr_t mutexattr) - int pthread_mutex_lock(pthread_mutex_t mutex)
- int pthread_mutex_trylock(pthread_mutex_t
mutex) - int pthread_mutex_unlock(pthread_mutex_t mutex)
- int pthread_mutex_destroy(pthread_mutex_t
mutex)
8Typical Mutex controlled interactions
- Set up maybe in the main thread before creating
other threads - status pthread_mutex_init (prime_list, NULL)
- Individual threads try to
- Lock the mutex
- status pthread_mutex_lock (prime_list)
- Note this thread blocks until it gets the mutex
- Mutually exclusive access to some global data
structure (prime_list) - Unlock the mutex
- status pthread_mutex_unlock (prime_list)
- Terminate
- pthread_mutex_destroy(pthread_mutex_t mutex)
9Mutex Errno Codes
- pthread_mutex_lock
- EINVAL - the mutex has not been properly
initialized. - EDEADLK - the mutex is already locked by
the calling thread - error checking'' mutexes only)
- pthread_mutex_trylock
- EBUSY- the mutex was currently locked.
- EINVAL - the mutex has not been properly
initialized. - pthread_mutex_unlock
- EINVAL - the mutex has not been properly
initialized. - EPERM - the calling thread does not own the mutex
- pthread_mutex_destroy
- EBUSY - the mutex is currently locked.
10Condition Variables
- Very often we encounter situations where a thread
needs to wait on a certain condition to be met
before it can proceed with its own task. A
typical situation would be where one thread
requires data from another thread before it can
proceed. - One approach would be to have one thread
constantly polling (possibly in a critical
section), to check if the condition is met.
However this can be very resource consuming since
that thread is continuously busy in this
activity. A condition variable is a way to
achieve the same goal. - A condition variable is used in conjunction with
a mutex lock and a predicate (typically a Boolean
variable) to allow one thread to signal (or wake
up) other threads which are waiting on that
condition variable.
Vijay Sonnad - IBM Corporation
http//www.pdc.kth.se/training/Talks/SMP/maui98/s
onnad/threads_only/talk/lecture.html
11Typical processing of condition variables
- Set up
- Create a condition variable
- Create an associated mutex and define a predicate
variable - Waiting on a condition variable
- Lock the mutex
- While predicate is unchanged wait on condition
variable - Do work
- Unlock the mutex
- Signalling on a condition variable
- Lock the mutex
- Do work
- Change predicate
- Signal on the condition variable
- Unlock mutex
Vijay Sonnad - IBM Corporation
http//www.pdc.kth.se/training/Talks/SMP/maui98/s
onnad/threads_only/talk/lecture.html
12Condition Variable Functions
- int pthread_cond_init(pthread_cond_t cond,
pthread_condattr_t cond_attr) - int pthread_cond_signal(pthread_cond_t cond)
- int pthread_cond_broadcast(pthread_cond_t cond)
- int pthread_cond_wait(pthread_cond_t cond,
pthread_mutex_t mutex) - int pthread_cond_timedwait(pthread_cond_t
cond, pthread_mutex_t mutex, const struct
timespec abstime) - int pthread_cond_destroy(pthread_cond_t cond)
13Attributes for threads
- /usr/include/bits/pthreadtypes.h
- typedef struct __pthread_attr_s
-
- int __detachstate // is this thread joinable
or not - int __schedpolicy // scheduling policy
- SCHED_RR round robin scheduling
- SCHED_FIFO first in first out
- SCHED_OTHER - regular, non-realtime scheduling
- struct __sched_param __schedparam //
scheduling priority - int __inheritsched
- int __scope
- size_t __guardsize
- int __stackaddr_set
- void __stackaddr
- size_t __stacksize
- pthread_attr_t
14Last Program Concurrent Web Server