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