1 #include <stdio.h>
2 #include <assert.h>
3 #include "thread-test.h"
4
5 #define NTHREADS 10
6
7
8 extern int add1(int, int);
9
10 static sem_t done;
11
12
start_routine(void * arg)13 static void *start_routine(void *arg)
14 {
15 int i, x, expected, status;
16
17 expected = add1(40, 2);
18 assert((expected % 1000) == 42);
19
20 for (i=0; i<10; i++) {
21 x = add1(50, i);
22 assert(x == expected + 8 + i);
23 }
24
25 status = sem_post(&done);
26 assert(status == 0);
27
28 return arg;
29 }
30
main(void)31 int main(void)
32 {
33 pthread_t th;
34 int i, status = sem_init(&done, 0, 0);
35 assert(status == 0);
36
37 for (i = 0; i < NTHREADS; i++) {
38 status = pthread_create(&th, NULL, start_routine, NULL);
39 assert(status == 0);
40 }
41 for (i = 0; i < NTHREADS; i++) {
42 status = sem_wait(&done);
43 assert(status == 0);
44 }
45 printf("done\n");
46 return 0;
47 }
48