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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// !> Thread-safe queue with independent head and tail

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include <unistd.h>

#include <assert.h>

typedef struct node {
  int value;
  struct node *next;
} node_t;

typedef struct queue {
  node_t *head;
  node_t *tail;
  pthread_mutex_t head_lock;
  pthread_mutex_t tail_lock;
} queue_t;


queue_t *q; // make the queue shared among all threads

queue_t *queue_new() {

  queue_t *q = malloc(sizeof(queue_t));
  assert(q != NULL);

  // insert a dummy head node
  node_t *tmp = malloc(sizeof(node_t));
  assert(tmp != NULL);
  tmp->next = NULL;

  assert(0 == pthread_mutex_init(&q->head_lock, NULL));
  assert(0 == pthread_mutex_init(&q->tail_lock, NULL));
  q->head = q->tail = tmp;

  return q;
}

int queue_empty(queue_t *q) {
  assert(q != NULL);
  return q->head->next == NULL;
}


/**
 * Thread-safe enqueue.
 *
 * Insert the given value to the back of the queue.
 */
void queue_enqueue(queue_t *q, int value) {
  assert(q != NULL);

  node_t *tmp = malloc(sizeof(node_t));
  assert(tmp != NULL);
  tmp->value = value;
  tmp->next = NULL;

  assert(0 == pthread_mutex_lock(&q->tail_lock));

  q->tail->next = tmp;
  q->tail = tmp;

  assert(0 == pthread_mutex_unlock(&q->tail_lock));
}


/**
 * Thread safe dequeue.
 *
 * Writes the dequeued value to the address given as the second argument and
 * returns 0 on success, or -1 if the queue was empty.
 */
int queue_dequeue(queue_t *q, int *value) {
  assert(q != NULL);
  
  assert(0 == pthread_mutex_lock(&q->head_lock));
  assert(q->head != NULL);

  node_t *tmp = q->head;
  node_t *new_head = tmp->next;

  if (new_head == NULL) {
    assert(0 == pthread_mutex_unlock(&q->head_lock));
    return -1; // queue was empty
  }

  *value = new_head->value;
  q->head = new_head;

  free(tmp);
  assert(0 == pthread_mutex_unlock(&q->head_lock));
  return 0;
}

// TODO: void queue_free(queue_t * q) { ... }

void *reader_thread(void *arg) {
  int me = *((int *) arg);

  int val;

  sleep(2);
  while (queue_dequeue(q, &val) == 0) {
    printf("%d: %d\n", me, val);
  }
  return NULL;
}

void *writer_thread(void *arg) {
  int from = *((int *) arg);

  sleep(1);
  for (int i = from; i < from + 10; i++) {
    queue_enqueue(q, i);
  }

  return NULL;
}

int main(int argc, char **argv) {
  
  q = queue_new();

  pthread_t threads[4];

  // start two writer threads
  int from1 = 0;
  assert(pthread_create(&threads[0], NULL, writer_thread, &from1) == 0);
  int from2 = 100;
  assert(pthread_create(&threads[2], NULL, writer_thread, &from2) == 0);

  // start two reader threads
  int reader1 = 1;
  assert(pthread_create(&threads[1], NULL, reader_thread, &reader1) == 0);
  int reader2 = 2;
  assert(pthread_create(&threads[3], NULL, reader_thread, &reader2) == 0);


  // wait for all threads to finish
  for (int i = 0; i < 4; i++) {
    assert(pthread_join(threads[i], NULL) == 0);
  }
  // TODO: queue_free(q);

  return 0;
}