6 #include "model-assert.h"
10 static thrd_t *threads;
11 static unsigned int *input;
12 static unsigned int *output;
13 static int num_threads;
17 thrd_t curr = thrd_current();
19 for (i = 0; i < num_threads; i++)
20 if (curr.priv == threads[i].priv)
27 static void main_task(void *param)
31 int pid = *((int *)param);
35 enqueue(queue, input[0]);
36 printf("Thrd %d Enqueue %d.\n", get_thread_num(), input[0]);
38 succ1 = dequeue(queue, &output[0]);
40 printf("Thrd %d: Dequeue %d.\n", get_thread_num(), output[0]);
42 printf("Thrd %d: Dequeue NULL.\n", get_thread_num());
44 } else if (pid % 2 == 1) {
46 enqueue(queue, input[1]);
47 printf("Thrd %d Enqueue %d.\n", get_thread_num(), input[1]);
49 succ2 = dequeue(queue, &output[1]);
51 printf("Thrd %d: Dequeue %d.\n", get_thread_num(), output[1]);
53 printf("Thrd %d: Dequeue NULL.\n", get_thread_num());
57 int user_main(int argc, char **argv)
63 unsigned int in_sum = 0, out_sum = 0;
65 queue = calloc(1, sizeof(*queue));
68 threads = malloc(num_threads * sizeof(thrd_t));
69 param = malloc(num_threads * sizeof(*param));
70 input = calloc(num_threads, sizeof(*input));
71 output = calloc(num_threads, sizeof(*output));
73 init_queue(queue, num_threads);
74 for (i = 0; i < num_threads; i++) {
76 thrd_create(&threads[i], main_task, ¶m[i]);
78 for (i = 0; i < num_threads; i++)
79 thrd_join(threads[i]);