SlideServe
Menu
Préférences
© SlideServe · v2026

Some Concurrent Patterns

tucker-webb avatar
13 diapositives · 349 Views · Nov 27, 2014

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.

Some Concurrent Patterns - slide 1
01 / 13Some Concurrent Patterns
1 / 17

Télécharger la présentation

Some Concurrent Patterns
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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