07.ConditionSynchronization

07.ConditionSynchronization - ConditionSynchronization 1...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Condition Synchronization
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel program. People are still trying to figure that out. Compromises: between making it easy to modify shared variables AND restricting when you can modify shared variables. between really flexible primitives AND simple primitives that are easy to reason about.
Background image of page 2
3 Beyond Locks Synchronizing on a condition. When you start working on a synchronization problem, first define the mutual exclusion constraints, then ask “when does a thread wait”, and create a separate synchronization variable representing each constraint. Bounded Buffer problem – producer puts things in a fixed sized buffer, consumer takes them out. What are the constraints for bounded buffer? 1) only one thread can manipulate buffer queue at a time ( mutual exclusion) 2) consumer must wait for producer to fill buffers if none full ( scheduling constraint) 3) producer must wait for consumer to empty buffers if all full ( scheduling constraint)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Beyond Locks Locks ensure mutual exclusion Bounded Buffer problem – producer puts things in a fixed sized buffer, consumer takes them out. Synchronizing on a condition. Class BoundedBuffer{     …     void* buffer[];     Lock lock;     int count = 0; } Class BoundedBuffer{     …     void* buffer[];     Lock lock;     int count = 0; } BoundedBuffer::Deposit(c){     lock acquire();     while (count == n); //spin     Add c to the buffer;     count++;     lock release(); } BoundedBuffer::Deposit(c){     lock acquire();      while (count == n); //spin     Add c to the buffer;     count++;     lock release(); } BoundedBuffer::Remove(c){     lock acquire();     while (count == 0); // spin     Remove c from buffer;     count--;     lock release(); } BoundedBuffer::Remove(c){     lock acquire();      while (count == 0); // spin     Remove c from buffer;     count--;     lock release(); } What is wrong  with this? What is wrong  with this?
Background image of page 4
5 Beyond Locks Class BoundedBuffer{     …     void* buffer[];     Lock lock;     int count = 0; } Class BoundedBuffer{     …     void* buffer[];     Lock lock;     int count = 0; } BoundedBuffer::Deposit(c){     while (count == n); //spin     lock acquire();     Add c to the buffer;     count++;     lock release(); } BoundedBuffer::Deposit(c){     while (count == n); //spin      lock acquire();     Add c to the buffer;     count++;     lock release();
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

07.ConditionSynchronization - ConditionSynchronization 1...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online