Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: list.next should be non-null for condition to be satisfied

...

Code Block
bgColor#FFcccc
langc
#include <condition_variable>
#include <mutex>
 
struct Node {
  void *node;
  struct Node *next;
};
  
static Node list;
static std::mutex m;
static std::condition_variable condition;
  
void consume_list_element(std::condition_variable &condition) {
  std::unique_lock<std::mutex> lk(m);
  
  if (list.next == nullptr) {
    condition.wait(lk);
  }
 
  // Proceed when condition holds.
}

...

Code Block
bgColor#ccccff
langc
#include <condition_variable>
#include <mutex>
 
struct Node {
  void *node;
  struct Node *next;
};
  
static Node list;
static std::mutex m;
static std::condition_variable condition;
  
void consume_list_element() {
  std::unique_lock<std::mutex> lk(m);
  
  while (list.next == nullptr) {
    condition.wait(lk);
  }
 
  // Proceed when condition holds.
}

...