- threads - the other concurrency
- the basics
- threads and processes - lightweight
- dynamic and cheap creation
- cheap synchronization
- concurrent execution - where's the scheduler
- pre-emption - both intra- and inter-process
- address spaces - shared global, private stack
- other process resources shared - memory, locks, files
- threads don't own resources usually, its process does.
- the good
- cheap - thread operations are fast and resource light
- simple sharing - global sharing is automatic; restricted sharing
requires work
- the bad
- no protection - global sharing, remember
- thread safety - system calls
-
gethostbyname()
, gethostbyaddr()
, gethostent()
- use
static storage
-
gethostbyname_r()
, gethostbyaddr_r()
, gethostent_r()
- use
thread-supplied buffers
- support may be iffy - debugging, performance
- the issues
- resource ownership - thread allocated resources belong to the host
process
- is the os thread aware - what do blocking calls block
- from where do threads come?
- some language are thread-aware - java, modula-3
- some operating systems supply threads - solaris, nt
- some libraries supply threads - pthreads, cthreads
- multi-threaded architectures
- vertical or horizontal or some combination
- vertical - thread per request or session or client; each thread
sheppards a request from function to function
- horizontal - thread per function; each request is handed off from one
function to the next
- comparisons
- vertical is easier to design (implement the state machine), has more
complex sharing (unpredictable shared demand), harder to tune
(monolithic)
- horizontal is harder to design (state partitioning); has simpler
sharing (thread hand-off via queues), easier to tune (computations
are broken out and isolated)
- modern high-performance architectures seem to be going toward a
horizontal architecture
- the posix interface
- management
-
pthread_create(tid, attrs, fun, arg)
- create a new thread
-
pthread_join(tid, status)
- wait for thread to terminate
-
pthread_detach(tid)
- no parent rendesvous
-
pthread_exit(status)
- self-termination
-
pthread_cancel(tid)
- terminate a thread
- synchronization
- mutexes - binary semaphores, mutual exclusion
-
pthread_mutex_init(mutex, attrs)
- create mutex semaphore
-
pthread_mutex_destroy(mutex)
- delete a mutex
-
pthread_mutex_lock(mutex)
- lock the mutex; block if locked
-
pthread_mutex_trylock(mutex)
- lock the mutex; fail if locked
-
pthread_mutex_unlock(mutex)
- unlock the mutex
- condition variables - coordination
-
pthread_cond_init(cond, attrs)
- create a condition variable
-
pthread_cond_destroy(cond)
- delete a condition variable
-
pthread_cond_wait(cond, mutex)
- unlock the mutex and wait;
timed version too
-
pthread_cond_signal(cond)
- send a signal to a thread waiting on cond
-
pthread_cond_broadcast(cond)
- send a signal to all threads waiting on cond
- miscellaneous
- scheduling - contention scope, policy
- signals - signal masks
- attributes - threads, mutex
- termination functions - threads
This page last modified on 7 March 2002.