Thread Implementation More Pthread Programming - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Thread Implementation More Pthread Programming

Description:

Bound thread - allocated to a dedicated LWP. Unbound thread - migrate among a ... Recommendations (see Lewis&Berg pp. 70-71) Blocking in many-to-one problematic ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 18
Provided by: r335
Category:

less

Transcript and Presenter's Notes

Title: Thread Implementation More Pthread Programming


1
Thread ImplementationMore Pthread Programming
2
Agenda
  • Scheduling programming guides
  • Synchronization
  • More Pthreads programming

3
Threads and LWPs
  • Two types of threads
  • Bound thread - allocated to a dedicated LWP
  • Unbound thread - migrate among a set of LWPs
  • How many LWPs? Recommendations
  • If completely CPU-bound, one LWP per CPU
  • If I/O bound, number of concurrent blocking I/O
    calls
  • If mostly CPU bound, but some I/O, one LWP per
    CPU one for each concurrent blocking I/O calls
  • In practice, what you tell the thread library (if
    it offers this option) is only a hint you may
    get more or less than what you requested
  • int Pthread_setconcurrency(int new_level)

4
Bound vs. Unbound Threads
  • Recommendations (see LewisBerg pp. 70-71)
  • Blocking in many-to-one problematic
  • System contention scope (bound threads,
    one-to-one)
  • Recommended approach for most applications
    simpler and usually works fine
  • Drawbacks expensive creation, limited number,
    but
  • Solaris has fast LWP switching Linux threads
    one-to-one anyway
  • Memory is cheap! Adjust thread stack size if
    memory constrained
  • Process contention scope (unbound threads,
    many-to-many)
  • Use if you need a large number of threads (more
    than the number of LWPs you can get, e.g.,
    thousands)

5
Example Pthreads
  • Specify thread is bound (system scope) or unbound
    (process scope) at creation
  • pthread_attr_t attr // parameters
  • pthread_attr_init(attr) // dont forget this!
  • pthread_attr_setscope(attr,PTHREAD_SCOPE_SYSTEM)
  • or
  • pthread_attr_setscope(attr,PTHREAD_SCOPE_PROCESS
    )
  • pthread_create(NULL, attr, foo, NULL)
  • pthread_attr_destroy(attr) // clean up

6
Scheduling
  • Priority level
  • Within a priority level
  • FIFO
  • Round Robin (FIFO timeslicing)
  • Details depend on implementation
  • Pthreads
  • int pthread_attr_setschedpolicy(attr,SCHED_FIFO)
  • int pthread_attr_setschedpolicy(attr,SCHED_RR)
  • int pthread_attr_setschedpolicy(attr,SCHED_OTHER)
  • pthread_attr_setschedparam to set priority
  • In practice, dont need to worry about this
    unless working with tight real-time constraints
    (e.g., response time lt 100 milliseconds) there
    are many more issues when dealing with real-time

7
Agenda
  • Scheduling programming guides
  • Synchronization
  • More Pthreads programming

8
Synchronization
  • Hardware support provided in most machines
    (uniprocessors as well as multiprocessors)
  • Interrupt can occur between any two instructions
  • Test and set primitive reads a register and then
    sets it, all as one atomic operation
  • If multiple CPUs simultaneous try to set the
    register, only one will succeed (have 0 returned)
  • Example SPARC
  • // load and store unsigned byte
  • // load byte into register, set it to 0xFF
  • ldstub address -gt register

9
Example Mutex Lock(SPARC)
  • try_again
  • ldstub address -gt register
  • compare register, 0
  • branch_equal got_it
  • call go_to_sleep
  • jumpo try_again
  • got_it
  • return

10
Cross-Process Synchronization Variables
  • Synchronization variables can be used to
    synchronize processes
  • Pthreads
  • Place variable in shared memory (can be stored in
    a file mapped to the processs address space)
  • Both processes must know about the variable
  • Exactly one process must initialize the variable
  • Processes can be single or multi threaded
  • Performance?

11
Timings
Note Timings are dated, for 167 MHz Ultrasparc 1
12
Agenda
  • Scheduling programming guides
  • Synchronization
  • More Pthreads programming

13
Pthreads Detached Threads
  • Non-detached (default) can be joined
  • Does not disappear when the thread exits
    (resources not released)!
  • Only reclaims resources when it is joined
  • Detached
  • Cannot be joined
  • Cleans itself up after it exits
  • Actually, more common case
  • Can call pthread_detach() to become detached
  • Make it detached at initialization (recommended)
  • pthread_attr_t attr
  • pthread_attr_init(attr)
  • pthread_attr_setdetachstate(attr,PTHREAD_CREATE_D
    ETACHED)
  • Pthread_create( attr, )

14
Pthreads Variables
  • Mutex variables
  • int pthread_mutex_init ( // dynamic
    initialization
  • pthread_mutex_t mutex,
  • const pthread_mutexattr_t attr)
  • // default attributes probably OK
  • int pthread_mutex_destroy ( // clean up
  • pthread_mutex_t mutex)
  • int pthread_mutex_trylock ( // return 0 if got
    lock
  • pthread_mutex_t mutex) // return EBUSY if
    failed
  • Condition variables
  • int pthread_cond_init ( // dynamic
    initialization
  • pthread_cond_t cond,
  • const pthread_condattr_t attr)
  • int pthread_cond_destroy ( // clean up
  • pthread_cond_t cond)
  • Destructors must be called before freeing space
    used by a malloced synchronization variable
    (beware of dangling pointers!)

15
Common Pthreads Errors
  • Forget to check return values for errors
  • Not joining non-detached threads (memory leak)
  • Using library functions that are not
    multithreading (MT) safe
  • If it doesnt say MT safe you can assume it
    isnt
  • Falling off bottom of main()
  • Results in calling exit() which exits entire
    program
  • Call pthread_exit() unless program finished
  • Assuming bit, byte, or word stores are atomic
  • They may or may not be!
  • Protect all shared data with a mutex

16
More Pthread Errors
  • Passing pointer to stack data to another thread
  • foo()
  • my_struct s
  • s.data get_data()
  • pthread_create( s, )
  • pthread_exit()
  • Whats wrong with this?

17
More Info on Pthreads
  • We are just covering the basics here
  • See pointers on course web page (some a little
    dated)
  • See man pages for details
  • Buy one of the recommended pthreads books!
Write a Comment
User Comments (0)
About PowerShow.com