1 / 18

Pthread II

Pthread II. Outline. Join Mutex Variables Condition Variables. Join Mutex Variables Condition Variables. "Joining" is one way to accomplish synchronization between threads. Declare a pthread attribute variable of the pthread_attr_t data type

britain
Download Presentation

Pthread II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pthread II

  2. Outline • Join • Mutex Variables • Condition Variables

  3. Join • Mutex Variables • Condition Variables

  4. "Joining" is one way to accomplish synchronization between threads.

  5. Declare a pthread attribute variable of the pthread_attr_t data type • Initialize the attribute variable with pthread_attr_init() • Set the attribute detached status with pthread_attr_setdetachstate() • When done, free library resources used by the attribute with pthread_attr_destroy()

  6. int pthread_join( pthread_t tid, void **status ); • If successful, the pthread_join() function shall return zero; • otherwise, an error number shall be returned to indicate the error (the number>0)

  7. Join • Mutex Variables • Condition Variables

  8. Typical Sequence • Create and initialize a mutex variable • Several threads attempt to lock the mutex • Only one succeeds and that thread owns the mutex • The owner thread performs some set of actions • The owner unlocks the mutex • Another thread acquires the mutex and repeats the process • Finally the mutex is destroyed

  9. Creating and Destroying Mutexes • pthread_mutex_init (mutex,attr) • pthread_mutex_destroy (mutex) • pthread_mutexattr_init (attr) • pthread_mutexattr_destroy (attr)

  10. Initialize Mutex variables must be declared with type pthread_mutex_t , and must be initialized before they can be used. 1. pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; 2. pthread_mutex_init() routine. This method permits setting mutex object attributes, attr The mutex is initially unlocked

  11. Locking and Unlocking Mutexes • pthread_mutex_lock (mutex) • pthread_mutex_trylock (mutex) • pthread_mutex_unlock (mutex)

  12. pthread_mutex_unlock() will unlock a mutex if called by the owning thread An error will be returned if: • If the mutex was already unlocked • If the mutex is owned by another thread

  13. The programmer have to to insure that the necessary threads all make the the mutex lock and unlock calls correctly • E.g. lock ….. lock Deadlock

  14. Join • Mutex Variables • Condition Variables

  15. pthread_cond_init (condition,attr) • pthread_cond_destroy (condition) • pthread_condattr_init (attr) • pthread_condattr_destroy (attr)

  16. Condition variable initialization • pthread_cond_t myconvar = PTHREAD_COND_INITIALIZER; • pthread_cond_init() The ID of the created condition variable is returned to the calling thread through the condition parameter

  17. Waiting and Signaling on Condition Variables • pthread_cond_wait (condition,mutex) • pthread_cond_signal (condition) • pthread_cond_broadcast (condition)

  18. more than one thread is in a blocking wait state =>pthread_cond_broadcast() • It is a logical error to call pthread_cond_signal() before calling pthread_cond_wait()

More Related