10.SumAndReadersWriters

10.SumAndReadersWriters - 1 Concurrent Programming Issues...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 Concurrent Programming Issues & Readers/Writers 2 Summary of Our Discussions Developing and debugging concurrent programs is hard Non-deterministic interleaving of instructions Safety: isolation and atomicity Scheduling: busy-waiting and blocking Synchronization constructs Locks: mutual exclusion Condition variables: wait while holding a lock Transactions: isolation by conflict detection and rollback, atomicity by buffering Semaphores: Mutual exclusion (binary) and condition synchronization (counting) How can you use these constructs effectively? Develop and follow strict programming style/strategy 3 Programming Strategy Decompose the problem into objects Object-oriented style of programming Identify shared chunk of state Encapsulate shared state and synchronization variables inside objects Dont manipulate shared variables or synchronization variables along with the logic associated with a thread Programs with race conditions always fail. A. True, B. False 4 General Programming Strategy Two step process Threads: Identify units of concurrency these are your threads Identify chunks of shared state make each shared thing an object; identify methods for these objects (how will the thread access the objects?) Write down the main loop for the thread Shared objects: Identify synchronization constructs Mutual exclusion vs. conditional synchronization Create a lock/condition variable for each constraint Develop the methods using locks and condition variables for coordination 5 Coding Style and Standards Always do things the same way Always use locks and condition variables Always hold locks while operating on condition variables Always acquire lock at the beginning of a procedure and release it at the end If it does not make sense to do this split your procedures further Always use while to check conditions, not if (Almost) never sleep(), yield(), or isLocked() in your code Use condition variables to synchronize while (predicate on state variable) { conditionVariable wait(&lock); }; while (predicate on state variable) { conditionVariable wait(&lock); }; 6 Readers/Writers: A Complete Example Motivation Shared databases accesses Examples: bank accounts, airline seats, Two types of users Readers: Never modify data Writers: read and modify data Problem constraints Using a single lock is too restrictive Allow multiple readers at the same time but only one writer at any time Specific constraints Readers can access database when there are no writers Writers can access database when there are no readers/writers Only one thread can manipulate shared variables at any time 7 Readers/Writer: Solution Structure Basic structure: two methods Database::Read() { Wait until no writers; Block any writers;...
View Full Document

Page1 / 14

10.SumAndReadersWriters - 1 Concurrent Programming Issues...

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

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