AlexLecture_Synchronization

AlexLecture_Synchronization - Concurrent access to shared...

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

View Full Document Right Arrow Icon
1 Synchronization 2 Concurrent access to shared data s Problem: Multiple active objects might access the same passive object “at the same time” ─ Generally OK if the active objects are only reading data from the passive object(s) but even this can be dangerous if the “reader” methods use iterators or some other mutable operation ─ But if one or more active objects is modifying the data members of the shared object, then we get anomalies s Example: Two active objects trying to pull elements from the same queue s To prevent these data-access anomalies requires synchronizing the active objects. 3 Example class Queue { public: { bool retval = q. empty (); if (!retval) { s = q. back (); q. pop (); } return retval; } protected: queue<string> q; }; 4 Example pull pull : Queue actor1 : … actor2 : … Suppose Queue initially contains <“hello”, “world”, “foo”>. What are the possible outcomes?
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 5 Possible outcomes s Actor1 gets “hello”, actor2 gets “world”, queue contains <“foo”> s Actor1 gets “world”, actor2 gets “hello”, queue contains <“foo”> s Both actor1 and actor2 get “hello”, queue contains <“foo”> s Other possibilities, including corruption of internal state of queue. .. 6 Question: Is this interaction possible? pull pull : Queue actor1 : … actor2 : … 7 The sad news… s While the use of multiple threads is very powerful, to avoid errors requires: ─ Reasoning that “breaks” modularity, i.e., thinking about how methods are implemented and how methods they invoke are implemented ─ Reasoning about a large number of possible thread interleavings 8 Example interleaving :Queue actor2 : … actor1 : … pull empty back pop pull empty back pop Question: What happens if queue contains A ?
Background image of page 2
3 9 Example interleaving :Queue actor2 : … actor1 : … pull empty back pop pull empty back pop Question: What happens if queue contains A, B ? 10 What can go wrong here? Queue contains: A,B,C :Queue actor2 : … actor1 : … pull empty back pop pull empty back pop 11 To prevent unsafe interleavings s Promote shared object into a monitor ─ high-level synchronization construct ─ contains an implicit lock – only one thread can be executing within the monitor at one time s Concurrent activations of monitor operations: ─ execute in some order without overlap ─ i.e., are serialized but the exact order of execution is not defined 12 Potential scenario :MonitorQueue actor2 : … actor1 : … pull empty back pop pull empty back pop
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 13 Another potential scenario :MonitorQueue actor2 : … actor1 : …
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

AlexLecture_Synchronization - Concurrent access to shared...

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

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