Lecture Notes for Client-Server Interfaces

5 March 2002 - Thread-Based Concurrency


  1. threads - the other concurrency

  2. the basics

    1. threads and processes - lightweight

    2. dynamic and cheap creation

    3. cheap synchronization

    4. concurrent execution - where's the scheduler

    5. pre-emption - both intra- and inter-process

    6. address spaces - shared global, private stack

    7. other process resources shared - memory, locks, files

    8. threads don't own resources usually, its process does.

  3. the good

    1. cheap - thread operations are fast and resource light

    2. simple sharing - global sharing is automatic; restricted sharing requires work

  4. the bad

    1. no protection - global sharing, remember

    2. thread safety - system calls

      1. gethostbyname(), gethostbyaddr(), gethostent() - use static storage

      2. gethostbyname_r(), gethostbyaddr_r(), gethostent_r() - use thread-supplied buffers

    3. support may be iffy - debugging, performance

  5. the issues

    1. resource ownership - thread allocated resources belong to the host process

    2. is the os thread aware - what do blocking calls block

  6. from where do threads come?

    1. some language are thread-aware - java, modula-3

    2. some operating systems supply threads - solaris, nt

    3. some libraries supply threads - pthreads, cthreads

  7. multi-threaded architectures

    1. vertical or horizontal or some combination

    2. vertical - thread per request or session or client; each thread sheppards a request from function to function

    3. horizontal - thread per function; each request is handed off from one function to the next

    4. comparisons

      1. vertical is easier to design (implement the state machine), has more complex sharing (unpredictable shared demand), harder to tune (monolithic)

      2. horizontal is harder to design (state partitioning); has simpler sharing (thread hand-off via queues), easier to tune (computations are broken out and isolated)

      3. modern high-performance architectures seem to be going toward a horizontal architecture

  8. the posix interface

    1. management

      1. pthread_create(tid, attrs, fun, arg) - create a new thread

      2. pthread_join(tid, status) - wait for thread to terminate

      3. pthread_detach(tid) - no parent rendesvous

      4. pthread_exit(status) - self-termination

      5. pthread_cancel(tid) - terminate a thread

    2. synchronization

      1. mutexes - binary semaphores, mutual exclusion

        1. pthread_mutex_init(mutex, attrs) - create mutex semaphore

        2. pthread_mutex_destroy(mutex) - delete a mutex

        3. pthread_mutex_lock(mutex) - lock the mutex; block if locked

        4. pthread_mutex_trylock(mutex) - lock the mutex; fail if locked

        5. pthread_mutex_unlock(mutex) - unlock the mutex

      2. condition variables - coordination

        1. pthread_cond_init(cond, attrs) - create a condition variable

        2. pthread_cond_destroy(cond) - delete a condition variable

        3. pthread_cond_wait(cond, mutex) - unlock the mutex and wait; timed version too

        4. pthread_cond_signal(cond) - send a signal to a thread waiting on cond

        5. pthread_cond_broadcast(cond) - send a signal to all threads waiting on cond

    3. miscellaneous

      1. scheduling - contention scope, policy

      2. signals - signal masks

      3. attributes - threads, mutex

      4. termination functions - threads


This page last modified on 7 March 2002.