Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langc
#include <stddef.h>
#include <threads.h>
 
struct node_t {
  void *node;
  struct node_t *next;
};
 
struct node_t list;
static mtx_t lock;
static cnd_t condition;
 
void consume_list_element(void) {
  if (thrd_success != mtx_lock(&lock)) {
    /* Handle error */
  }
 
  if (list.next == NULL) {
    if (thrd_success != cnd_wait(&condition, &lock)) {
      /* Handle error */
    }
  }

  /* Proceed when condition holds */

  if (thrd_success != mtx_unlock(&lock)) {
    /* Handle error */
  }
}

...

Code Block
bgColor#ccccff
langc
#include <stddef.h>
#include <threads.h>
 
struct node_t {
  void *node;
  struct node_t *next;
};
 
struct node_t list;
static mtx_t lock;
static cnd_t condition;
 
void consume_list_element(void) {
  if (thrd_success != mtx_lock(&lock)) {
    /* Handle error */
  }
 
  while (list.next == NULL) {
    if (thrd_success != cnd_wait(&condition, &lock)) {
      /* Handle error */
    }
  }

  /* Proceed when condition holds */

  if (thrd_success != mtx_unlock(&lock)) {
    /* Handle error */
  }
}

...