Locks and Condition Variables in Producer/Consumer Threads

Locks and Condition Variables in Producer/Consumer Threads
Slide Note
Embed
Share

In this technical content, we delve into the implementation of locks and condition variables in managing producer/consumer threads. The code snippets showcase the use of mutex for synchronization and illustrate different versions of the Pipe class for handling data flow between threads effectively. The discussions include scenarios dealing with shared resources, thread safety, and ensuring proper synchronization using mutex and condition variables.

  • Threads
  • Synchronization
  • Mutex
  • Condition Variables
  • Producer/Consumer

Uploaded on Feb 27, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Too Much Milk With Locks Both threads: std::mutex mutex; ... mutex.lock(); if (milk == 0) { buy_milk(); } mutex.unlock(); CS 111 Lecture Notes: Locks and Condition Variables Slide 1

  2. Producer/Consumer, v1 class Pipe { Pipe() : count(0), nextPut(0), nextGet(0) {} void put(char c); char get(); char Pipe::get() { char c; mutex.lock(); count--; c = buffer[nextGet]; nextGet++; if (nextGet == SIZE) { nextGet = 0; } mutex.unlock(); return c; } std::mutex mutex; char buffer[SIZE]; int count; int nextPut; int nextGet; }; void Pipe::put(char c) { mutex.lock(); count++; buffer[nextPut] = c; nextPut++; if (nextPut == SIZE) { nextPut = 0; } mutex.unlock(); } CS 111 Lecture Notes: Locks and Condition Variables Slide 2

  3. Producer/Consumer, v2 class Pipe { Pipe() : count(0), nextPut(0), nextGet(0) {} void put(char c); char get(); char Pipe::get() { char c; mutex.lock(); while (count == 0) { mutex.unlock(); mutex.lock(); } count--; c = buffer[nextGet]; nextGet++; if (nextGet == SIZE) { nextGet = 0; } mutex.unlock(); return c; } std::mutex mutex; char buffer[SIZE]; int count; int nextPut; int nextGet; }; void Pipe::put(char c) { mutex.lock(); while (count == SIZE) { mutex.unlock(); mutex.lock(); } count++; buffer[nextPut] = c; nextPut++; if (nextPut == SIZE) { nextPut = 0; } mutex.unlock(); } CS 111 Lecture Notes: Locks and Condition Variables Slide 3

  4. Producer/Consumer, v3 class Pipe { Pipe() : count(0), nextPut(0), nextGet(0) {} void put(char c); char get(); char Pipe::get() { char c; mutex.lock(); while (count == 0) { charAdded.wait(mutex); } count--; c = buffer[nextGet]; nextGet++; if (nextGet == SIZE) { nextGet = 0; } charRemoved.notify_one(); mutex.unlock(); return c; } std::mutex mutex; std::condition_variable charAdded, charRemoved; char buffer[SIZE]; int count; int nextPut; int nextGet; }; void Pipe::put(char c) { mutex.lock(); while (count == SIZE) { charRemoved.wait(mutex); } count++; buffer[nextPut] = c; nextPut++; if (nextPut == SIZE) { nextPut = 0; } charAdded.notify_one(); mutex.unlock(); } T1 T2 T3 CS 111 Lecture Notes: Locks and Condition Variables Slide 4

More Related Content