Threads
- Motivation
Keep programs interactive
Background save
Multiple clients
Basic idea
multiple lightweight processes
single address space
synchronization primitives available
i/o services available
api can be adapted to program’s needs
So what’s a thread?
stack
program counter (perhaps pushed onto stack?)
what about i/o, signals, global variables (errno?)?
Is it managed from user or kernel?
user threads have very cheap task context switch
kernel threads handle blocking i/o cleanly
extended models for i/o can simplify user threads
select() indicates which files are ready to transfer data
i/o via control block, special signal indicates completion
preemption can be implemented via signal
- should threads be preempted?
- easier programming model if processes yield() the processor.
- nuisance to program with extra yield() calls
- preemption can be controlled with special noprempt regions
So how do you use threads?
User interface/computation/io handled seperately (browser)
pop-up server threads
Does it make a difference whether threads may execute in parallel?
depends if there’s pre-emption
Thread primitives (case study: Distributed Computing Environment’s (DCE) thread package.
execution management
create/exit/join/detach
thread template
handy receptacle for common thread params
create/delete
priority
stack size
mutex params
mutex
create/delete
lock/trylock/unlock
two flavors
- fast
- can deadlock on self
friendly
won’t deadlock on self
not defined whether count like semaphores or instead have signal/wait semantics
- OOPS!