1 / 13

Some Concurrent Patterns

Some Concurrent Patterns. Producer/Consumer Avoid lock-step with bounded buffers Pipeline: chain of producer/consumer pairs Sleeping Barber: a producer/consumer rendezvous Dining Philosophers acquiring multiple locks Readers and Writers. Producer/Consumer via Semaphores.

tucker-webb
Download Presentation

Some Concurrent Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Some Concurrent Patterns • Producer/Consumer • Avoid lock-step with bounded buffers • Pipeline: chain of producer/consumer pairs • Sleeping Barber: a producer/consumer rendezvous • Dining Philosophers • acquiring multiple locks • Readers and Writers

  2. Producer/Consumer via Semaphores typeT buf; // a buffer of type Tsem empty = 1, full = 0; [Producer thread] { typeT prodData; while (true) { // produce prodData P(empty); buf = prodData; V(full); } } [Consumer thread] { typeT consData; while (true) { P(full); consData = buf; V(empty); // consume consData } }

  3. Bounded Buffer typeT buf[n]; // n buffers of type Tsem empty = n, full = 0; int front = 0, rear = 0; [Producer thread] { typeT prodData; while (true) { // produce prodData P(empty); buf[rear] = prodData; rear = (rear + 1) % n; V(full); } } [Consumer thread] { typeT consData; while (true) { P(full); consData = buf[front]; front = (front + 1) % n; V(empty); // consume consData } }

  4. Pipeline (Assembly Line) Input Thread A Thread B Thread C Output

  5. Dining Philosophers • Typical failure: deadlock • Workaround: asymmetric lock acquisition • Alternate approach: allow only n-1 to eat at same time

  6. The Wrong Solution sem fork[5] = {1, 1, 1, 1, 1};Philosopher thread[i = 0 to 4] { while(true) { P(fork[i]); P(fork[i+1]); eat; V(fork[i]); V(fork[i+1]); think; }}

  7. Asymmetric Solution sem fork[5] = {1, 1, 1, 1, 1}; Philosopher thread[i = 0 to 3] { while(true) { P(fork[i]); P(fork[i+1]); eat; V(fork[i]); V(fork[i+1]); think; }} Philosopher thread[4] { while(true) { P(fork[0]); P(fork[4]); eat; V(fork[0]); V(fork[4]); think; }}

  8. Readers/Writers • Writers need exclusive access • Readers can allow other readers in • Overly simple: don’t allow multiple readers • Better: designate first, last readers to handle the lock for the group • Problem: writers may get starved out • Solution: let writers keep “place in line”

  9. Reader-Biased Solution sem rw = 1, mutexR = 1;int nr = 0; Reader thread[i = 1 to R] { while(true) { // other work here P(mutexR); nr = nr + 1; // if first reader if (nr == 1) P(rw); V(mutexR); do the reading; P(mutexR); nr = nr - 1; // if last reader if (nr == 0) V(rw); V(mutexR); }} Writer thread { while(true) { // other work here P(rw); do the writing; V(rw); }}

  10. More Concurrent Patterns • Workgroup: single group vs organizations • Organization = layers of work groups • Client/Server • Same as workgroup, but client initiates • Typically less structured fan-out vs workgroup • Example: Web servers and thread pools • Scheduler/Requester • Resource-driven client/server (e.g. OS)

  11. Work Group Input Thread A Thread B Thread C Output

  12. Client/Server Client Request Client Request Client Request Server Client Handler Client Handler Client Handler

  13. The Concurrency Trajectory Well Designed Concurrency - patterns - classic models - monitors Many implementations > Java > win32, .NET > C# Coordinated Concurrency - locks, semaphores - mutex, barriers One implementation > pthreads Concurrency - definitions - awareness of issues

More Related