Mutual%20exclusion%20--%20pthreads - PowerPoint PPT Presentation

About This Presentation
Title:

Mutual%20exclusion%20--%20pthreads

Description:

Using pthread mutex. pthread_mutex_lock(&StructLock) ; do ... Atomic read/writes. When is the following safe? X = Y. In Java 32-bits are guaranteed atomic, ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 9
Provided by: scie189
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Mutual%20exclusion%20--%20pthreads


1
Mutual exclusion -- pthreads
  • Declaration
  • pthread_mutex_t StructLock
  • Initialization
  • pthread_mutex_init(StructLock,NULL)
  • Recursive and debugging locks may also be
    available

2
Using pthread mutex
  • pthread_mutex_lock(StructLock)
  • do whatever needs to be done
  • but quickly
  • pthread_mutex_unlock(StructLock)

3
Pthread conditionals
  • Declaration
  • pthread_mutex_t StructTstLck
  • pthread_cond_t StructQueue
  • Initialization
  • pthread_mutex_init(StructTstLck, NULL)
  • pthread_cond_init(StructQueue, NULL)
  • No alternatives in most implementations

4
Typical conditional entry
  • pthread_mutex_lock(StructTstLck)
  • See if it is OK to go ahead
  • if (not OK)
  • pthread_cond_wait(StructQueue,
  • StructTstLck)
  • pthread_mutex_unlock(StructTstLck)

5
Paranoid conditional entry
  • pthread_mutex_lock(StructTstLck)
  • do
  • See if it is OK to go ahead
  • if (not OK)
  • pthread_cond_wait(StructQueue,
  • StructTstLck)
  • while (not OK)
  • pthread_mutex_unlock(StructTstLck)

6
Unlocking the cond
  • pthread_mutex_lock(StructTstLock)
  • Set up stuff for get another thread started
  • This may not be required
  • pthread_mutex_unlock(StructTstLock)
  • pthread_cond_signal(StructQueue)

7
Java
  • Mutex with synchronized methods
  • Conditionals with wait/notify
  • final methods in all objects
  • But in Java 5.0,
  • java.util.concurrent.locks

8
Atomic read/writes
  • When is the following safe?
  • X Y
  • In Java 32-bits are guaranteed atomic,
  • longer read/writes are not
  • In 5.0, java.util.concurrent.atomic
Write a Comment
User Comments (0)
About PowerShow.com