DOC PREVIEW
UT CS 372 - Concurrent Programming Issues: Summary

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Concurrent Programming Issues:Summary2Summary of Our DiscussionsDeveloping and debugging concurrent programs is hardÿ Non-deterministic interleaving of instructionsSynchronization constructsÿ Locks: mutual exclusionÿ Condition variables: conditional synchronizationÿ Other primitives:v Semaphoresu Binary vs. countingu Can be used for mutual exclusion and conditional synchronizationHow can you use these constructs effectively?ÿ Develop and follow strict programming style/strategy3Programming StrategyDecompose the problem into objectsObject-oriented style of programmingÿ Identify shared chunk of stateÿ Encapsulate shared state and synchronization variables insideobjectsDon’t manipulate shared variables or synchronizationvariables along with the logic associated with a thread4General Programming StrategyTwo step processThreads:ÿ Identify units of concurrency – these are your threadsÿ Identify chunks of shared state – make each shared “thing” anobject; identify methods for these objects (how will the threadaccess the objects?)ÿ Write down the main loop for the threadShared objects:ÿ Identify synchronization constructsv Mutual exclusion vs. conditional synchronizationÿ Create a lock/condition variable for each constraintÿ Develop the methods –using locks and condition variables – forcoordination5Coding Style and StandardsAlways do things the same wayAlways use locks and condition variablesAlways hold locks while operating on condition variablesAlways acquire lock at the beginning of a procedure and release itat the endÿ If it does not make sense to do this ‡ split your procedures furtherAlways use while to check conditions, not if(Almost) never sleep() in your codeÿ Use condition variables to synchronizewhile (predicate on state variable) { conditionVariable‡wait(&lock); };while (predicate on state variable) { conditionVariable‡wait(&lock); };6Readers/Writers: A Complete ExampleMotivationÿ Shared databases accessesv Examples: bank accounts, airline seats, …Two types of usersÿ Readers: Never modify dataÿ Writers: read and modify dataProblem constraintsÿ Using a single lock is too restrictivev Allow multiple readers at the same timev …but only one writer at any timeÿ Specific constraintsv Readers can access database when there are no writersv Writers can access database when there are no readers/writersv Only one thread can manipulate shared variables at any time7Readers/Writer: Solution StructureBasic structure: two methodsState variablesDatabase::Read() { Wait until no writers; Access database; check out – wake up waiting writers;}Database::Read() { Wait until no writers; Access database; check out – wake up waiting writers;}Database::Write() { Wait until no readers/writers; Access database; check out – wake up waiting readers/writers;}Database::Write() { Wait until no readers/writers; Access database; check out – wake up waiting readers/writers;}AR = 0; // # of active readersAW = 0; // # of active writersWR = 0; // # of waiting readersWW = 0; // # of waiting writersCondition okToRead;Condition okToWrite;Lock lock;AR = 0; // # of active readersAW = 0; // # of active writersWR = 0; // # of waiting readersWW = 0; // # of waiting writersCondition okToRead;Condition okToWrite;Lock


View Full Document

UT CS 372 - Concurrent Programming Issues: Summary

Documents in this Course
MapReduce

MapReduce

17 pages

Processes

Processes

19 pages

MapReduce

MapReduce

17 pages

Load more
Download Concurrent Programming Issues: Summary
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Concurrent Programming Issues: Summary and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Concurrent Programming Issues: Summary 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?