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)
28 static void main_task(void *param)
32 int pid = *((int *)param);
36 succ1 = dequeue(queue, &input[0]);
39 enqueue(queue, input[1]);
44 enqueue(queue, input[0]);
45 printf("Thrd %d Enqueue %d.\n", get_thread_num(), input[0]);
47 succ1 = dequeue(queue, &output[0]);
49 printf("Thrd %d: Dequeue %d.\n", get_thread_num(), output[0]);
51 printf("Thrd %d: Dequeue NULL.\n", get_thread_num());
53 } else if (pid % 2 == 1) {
55 enqueue(queue, input[1]);
56 printf("Thrd %d Enqueue %d.\n", get_thread_num(), input[1]);
58 succ2 = dequeue(queue, &output[1]);
60 printf("Thrd %d: Dequeue %d.\n", get_thread_num(), output[1]);
62 printf("Thrd %d: Dequeue NULL.\n", get_thread_num());
66 int user_main(int argc, char **argv)
75 unsigned int in_sum = 0, out_sum = 0;
77 queue = calloc(1, sizeof(*queue));
78 //MODEL_ASSERT(queue);
81 threads = malloc(num_threads * sizeof(thrd_t));
82 param = malloc(num_threads * sizeof(*param));
83 input = calloc(num_threads, sizeof(*input));
84 output = calloc(num_threads, sizeof(*output));
86 init_queue(queue, num_threads);
87 for (i = 0; i < num_threads; i++) {
89 thrd_create(&threads[i], main_task, ¶m[i]);
91 for (i = 0; i < num_threads; i++)
92 thrd_join(threads[i]);
94 for (i = 0; i < num_threads; i++) {
98 for (i = 0; i < num_threads; i++)
99 printf("input[%d] = %u\n", i, input[i]);
100 for (i = 0; i < num_threads; i++)
101 printf("output[%d] = %u\n", i, output[i]);
102 //MODEL_ASSERT(in_sum == out_sum);