Title: Introduction to Threads Programming
1Introduction to Threads Programming
- 1999. 4. 28.
- Kang Sung Tak
- DB Lab, CS Dept, KAIST.
- stkang_at_dbserver.kaist.ac.kr
2Contents
- Introduction
- Overview of Solaris Threads
- Basic APIs
- Thread-Specific Data
- Concurrency Control
- Synchronization
- Pthreads Features
- Reference
3Introduction(1/2)
- Problem of process
- creating a process is expensive (space, time)
- involves creating a new address space
- context switching is expensive
- complex communication between processes
- Threads
- share all possible things within a process
- creating a thread is cheap
- communication between the threads of one process
is simple
4Introduction(2/2)
- Implementation of a threads package
- user-level threads
- put the threads package entirely in user space
- customized scheduling algorithm is possible
- scale better
- blocking problem
- jacket
- kernel-level threads
- all threads are managed by kernel
- no blocking problem
- problem
- threads table (in kernel!)
- system calls
- hybrid style
5Overview of Solaris Threads(1/2)
- Standards
- posix threads (posix1003.4a)
- APIs specification for multithreaded programming
- Solaris threads
- hybrid style threads (user-level threads)
- LWP(lightweight processes) bridge the user level
and the kernel level - each process contains one or more LWPs
- each LWP runs one or more user threads
- terms
- unbound threads
- threads that are scheduled on the LWP pool
- bound threads
- permanently binded thread to an LWP
6Overview of Solaris Threads(2/2)
- Multithreaded system architecture
7Basic APIs(1/3)
- Create a thread
- int thr_create(void stack_base, size_t
stack_size, void (start_routine) (void), void
arg, long flags, thread_t new_thread) - set stack_base as NULL, stack_size as 0
- flags
- THR_DETACHED
- set this when you do not want to wait for the
thread to terminate - THR_SUSPENDED
- suspends the new thread until the thread is
started by thr_continue() - THR_BOUND
- permanently binds the new thread to an LWP
- THR_NEW_LWP
- increase the concurrency level for unbound
threads by one - return 0 when it completes successfully
8Basic APIs(2/3)
- Suspend or continue thread execution
- int thr_suspend(thread_t target_thread)
- int thr_continue(thread_t target_thread)
- Terminate a thread
- void thr_exit(void status)
- if the calling thread is not detached, the exit
status retained in status - Wait for thread termination
- int thr_join(thread_t wait_for, thread_t
departed, void status) - when wait_for is 0, then thr_join() waits for any
undetached thread - when departed is not NULL, it points it points to
a location that is set to the ID of the
terminated thread - a thread can wait until all nondaemon threads
have terminated - while (thr_join(0, 0, 0)0)
9Basic APIs(3/3)
include ltthread.hgt main() int result
thread_t helper int status thr_create(0,
0, fetch, result, 0, helper)
thr_join(helper, 0, status) void fetch(int
result) ... result value
thr_exit(0)
10Thread-Specific Data
- Thread-specific data
- very much like global data, except that it is
private to a thread - each thread-specific data item is associated with
a key - 3 APIs
- int thr_keycreate(thread_key_t keyp, void
(destructor) void(value)) - int thr_setspecific(thread_key_t key, void
value) - int thr_getspecific(thread_key_t key, void
valuep) - example
thread_key_t mywin_key thr_keycreate(mywin_key,
free_key) win (win_t)malloc(sizeof(win_t)) th
r_setspecific(mywin_key, win) void
free_key(void win) free(win)
11Concurrency Control
- Thread concurrency level
- unbound threads in a process might or might not
be required to be simultaneously active - APIs
- int thr_getconcurrency(void)
- int thr_setconcurrency(int new_level)
- Thread priority
- an unbound thread is usually scheduled using
simple priority levels - range from 0 (the least significant) to the
largetst integer - APIs
- int thr_getprio(thread_t target_thread, int pri)
- int thr_setprio(thread_t target_thread, int pri)
12Synchronization(1/4)
- Synchronization
- the only wat to ensure consistency of shared data
- objects
- mutex locks
- condition variables
- readers/writer locks
- semaphores
13Synchronization(2/4)
- Mutual exclusion locks
- ensuring that only one thread at a time executes
a critical section of code - APIs
- int mutex_init(mutex_t mp, int type, void arg)
- type
- USYNC_PROCESS the mutex can be used to
synchronize threads in this and other processes - USYNC_THREAD the mutex can be used to
synchronize threads in this process, only - the arg is currently ignored
- int mutex_lock(mutex_t mp)
- int mutex_unlock(mutex_t mp)
- int mutex_destory(mutex_t mp)
14Synchronization(3/4)
include ltthread.hgt mutex_t count_mutex int
count main() mutex_init(count_mutex,
USYNC_THREAD, NULL) ... increment_count()
mutex_lock(count_mutex) count
mutex_unlock(count_mutex)
15Synchronization(4/4)
- Semaphores
- non-negative integer count
- typically used to coordinate access to resources,
with the semaphore count initialized to the
number of free resources - binary semaphore is logically just like a mutex
- APIs
- int sema_init(sema_t sp, unsigned int count, int
type, void arg) - same as mutex_init
- int sema_wait(sema_t sp)
- int sema_post(sema_t sp)
- int sema_destroy(sema_t sp)
16Pthreads Features(1/2)
- Attribute objects
- when a function is called to create a threads
entity, an argument points to an attribute object
is required - an opaque data type allocated and returned by a
function call - code portability
- Cancellation
- an option when all further operations of a
related set of threads are undesirable or
unnecessary - to cancel all thread action, restore things to a
consistent state - Scheduling
- defines a policy and provides a mechanism for
controlling the scheduling of threads onto
processors - SCHED_FIFO, SCHED_RR, SCHED_OTHER
17Pthreads Features(2/2)
include ltpthread.hgt pthread_attr_t
tattr pthread_t tid int ret retpthread_attr_i
nit(tattr) retpthread_attr_setdetachstate(tatt
r, PTHREAD_THREAD_DETACHED) retpthread_create(t
id, attr, func, arg)
18Reference
- Reference directory
- //adam/kakarot/cs492/
- Solaris threads
- MulthreadedProgrammingGuide.pdf
- Pthreads and Solaris threads
- pthread_comparision.ps
- Sample program
- FileCopy.c
- Compile
- use option -lthread
- gcc FileCopy.c -lthread