Skip to content

Synchronization

Condition variable

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Two hungry threads, anxiously waiting for their turn to take soup
#include <mutex>
#include <thread>

int soup_servings = 10;
std::mutex slow_cooker_lid;

void hungry_person(int id) {
  int put_lid_back = 0;
  while (soup_servings > 0) {
    std::unique_lock<std::mutex> lid_lock(
        slow_cooker_lid);  // pick up the slow cooker lid
    if ((id == soup_servings % 2) &&
        (soup_servings > 0)) {  // is it your turn to take soup?
      --soup_servings;          // it's your turn; take some soup!
    } else {
      ++put_lid_back;  // it's not your turn; put the lid back...
    }
  }
  printf("Person %d put the lid back %u times.\n", id, put_lid_back);
}

int main() {
  std::thread hungry_threads[2];
  for (int i = 0; i < 2; ++i) {
    hungry_threads[i] = std::thread(hungry_person, i);
  }
  for (auto& ht : hungry_threads) {
    ht.join();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Two hungry threads, anxiously waiting for their turn to take soup
#include <condition_variable>
#include <mutex>
#include <thread>

int soup_servings = 10;
std::mutex slow_cooker_lid;
std::condition_variable soup_taken;

void hungry_person(int id) {
  int put_lid_back = 0;
  while (soup_servings > 0) {
    std::unique_lock<std::mutex> lid_lock(
        slow_cooker_lid);  // pick up the slow cooker lid
    while ((id != soup_servings % 2) &&
           (soup_servings > 0)) {  // is it your turn to take soup?
      ++put_lid_back;              // it's not your turn; put the lid back...
      soup_taken.wait(lid_lock);   // ...and wait...
    }
    if (soup_servings > 0) {
      --soup_servings;          // it's your turn; take some soup!
      lid_lock.unlock();        // put back the lid
      soup_taken.notify_one();  // notify another thread to take their turn
    }
  }
  printf("Person %d put the lid back %u times.\n", id, put_lid_back);
}

int main() {
  std::thread hungry_threads[2];
  for (int i = 0; i < 2; ++i) {
    hungry_threads[i] = std::thread(hungry_person, i);
  }
  for (auto& ht : hungry_threads) {
    ht.join();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Two hungry threads, anxiously waiting for their turn to take soup
#include <condition_variable>
#include <mutex>
#include <thread>

int soup_servings = 10;
std::mutex slow_cooker_lid;
std::condition_variable soup_taken;

void hungry_person(int id) {
  int put_lid_back = 0;
  while (soup_servings > 0) {
    std::unique_lock<std::mutex> lid_lock(
        slow_cooker_lid);  // pick up the slow cooker lid
    while ((id != soup_servings % 5) &&
           (soup_servings > 0)) {  // is it your turn to take soup?
      ++put_lid_back;              // it's not your turn; put the lid back...
      soup_taken.wait(lid_lock);   // ...and wait...
    }
    if (soup_servings > 0) {
      --soup_servings;          // it's your turn; take some soup!
      lid_lock.unlock();        // put back the lid
      soup_taken.notify_all();  // notify another threads to take their turn
    }
  }
  printf("Person %d put the lid back %u times.\n", id, put_lid_back);
}

int main() {
  std::thread hungry_threads[5];
  for (int i = 0; i < 5; ++i) {
    hungry_threads[i] = std::thread(hungry_person, i);
  }
  for (auto& ht : hungry_threads) {
    ht.join();
  }
}

Producer-consumer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Threads serving and eating soup
#include <queue>
#include <thread>

class ServingLine {
public:
  void serve_soup(int i) {
    soup_queue.push(i);
  }

  int take_soup() {
    int bowl = soup_queue.front();
    soup_queue.pop();
    return bowl;
  }

private:
  std::queue<int> soup_queue;
};

ServingLine serving_line = ServingLine();

void soup_producer() {
  for (int i = 0; i < 1000000; ++i) {  // serve a 10,000 bowls of soup
    serving_line.serve_soup(1);
  }
  serving_line.serve_soup(-1);  // indicate no more soup
  printf("Producer is done serving soup!\n");
}

void soup_consumer() {
  int soup_eaten = 0;
  while (true) {
    int bowl = serving_line.take_soup();
    if (bowl == -1) {  // check for last bowl of soup
      printf("Consumer ate %d bowls of soup.\n", soup_eaten);
      serving_line.serve_soup(-1);  // put back last bowl for other consumers to take
      return;
    } else {
      soup_eaten += bowl;  // eat the soup
    }
  }
}

int main() {
  std::thread olivia(soup_producer);
  std::thread barron(soup_consumer);
  std::thread steve(soup_consumer);
  olivia.join();
  barron.join();
  steve.join();
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Threads serving and eating soup
#include <condition_variable>
#include <mutex>
#include <queue>
#include <thread>

class ServingLine {
public:
  void serve_soup(int i) {
    std::unique_lock<std::mutex> ladle_lock(ladle);
    soup_queue.push(i);
    ladle_lock.unlock();
    soup_served.notify_one();
  }

  int take_soup() {
    std::unique_lock<std::mutex> ladle_lock(ladle);
    while (soup_queue.empty()) {
      soup_served.wait(ladle_lock);
    }
    int bowl = soup_queue.front();
    soup_queue.pop();
    return bowl;
  }

private:
  std::queue<int> soup_queue;
  std::mutex ladle;
  std::condition_variable soup_served;
};

ServingLine serving_line = ServingLine();

void soup_producer() {
  for (int i = 0; i < 10000; ++i) {  // serve a 10,000 bowls of soup
    serving_line.serve_soup(1);
  }
  serving_line.serve_soup(-1);  // indicate no more soup
  printf("Producer is done serving soup!\n");
}

void soup_consumer() {
  int soup_eaten = 0;
  while (true) {
    int bowl = serving_line.take_soup();
    if (bowl == -1) {  // check for last bowl of soup
      printf("Consumer ate %d bowls of soup.\n", soup_eaten);
      serving_line.serve_soup(-1);  // put back last bowl for other consumers to take
      return;
    } else {
      soup_eaten += bowl;  // eat the soup
    }
  }
}

int main() {
  std::thread olivia(soup_producer);
  std::thread barron(soup_consumer);
  std::thread steve(soup_consumer);
  olivia.join();
  barron.join();
  steve.join();
}

Semaphore

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Connecting cell phones to a charger
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>

class Semaphore {
public:
  Semaphore(unsigned long init_count) { count_ = init_count; }

  void acquire() {  // decrement the internal counter
    std::unique_lock<std::mutex> lck(m_);
    while (!count_) {
      cv_.wait(lck);
    }
    --count_;
  }

  void release() {  // increment the internal counter
    std::unique_lock<std::mutex> lck(m_);
    ++count_;
    lck.unlock();
    cv_.notify_one();
  }

private:
  std::mutex m_;
  std::condition_variable cv_;
  unsigned long count_;
};

Semaphore charger(4);

void cell_phone(int id) {
  charger.acquire();
  printf("Phone %d is charging...\n", id);
  srand(id);  // charge for "random" amount between 1-3 seconds
  std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 2000 + 1000));
  printf("Phone %d is DONE charging!\n", id);
  charger.release();
}

int main() {
  std::thread phones[10];
  for (int i = 0; i < 10; ++i) {
    phones[i] = std::thread(cell_phone, i);
  }
  for (auto& p : phones) {
    p.join();
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Connecting cell phones to a charger
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>

class Semaphore {
public:
  Semaphore(unsigned long init_count) { count_ = init_count; }

  void acquire() {  // decrement the internal counter
    std::unique_lock<std::mutex> lck(m_);
    while (!count_) {
      cv_.wait(lck);
    }
    --count_;
  }

  void release() {  // increment the internal counter
    std::unique_lock<std::mutex> lck(m_);
    ++count_;
    lck.unlock();
    cv_.notify_one();
  }

private:
  std::mutex m_;
  std::condition_variable cv_;
  unsigned long count_;
};

Semaphore charger(1);

void cell_phone(int id) {
  charger.acquire();
  printf("Phone %d is charging...\n", id);
  srand(id);  // charge for "random" amount between 1-3 seconds
  std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 2000 + 1000));
  printf("Phone %d is DONE charging!\n", id);
  charger.release();
}

int main() {
  std::thread phones[10];
  for (int i = 0; i < 10; ++i) {
    phones[i] = std::thread(cell_phone, i);
  }
  for (auto& p : phones) {
    p.join();
  }
}