• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <assert.h>
3 #include "thread-test.h"
4 
5 extern int add2(int, int, int);
6 extern int add3(int, int, int, int);
7 
8 static sem_t done;
9 
10 
start_routine_2(void * arg)11 static void *start_routine_2(void *arg)
12 {
13     int x, status;
14     x = add2(40, 2, 100);
15     assert(x == 142);
16 
17     status = sem_post(&done);
18     assert(status == 0);
19 
20     return arg;
21 }
22 
start_routine_3(void * arg)23 static void *start_routine_3(void *arg)
24 {
25     int x, status;
26     x = add3(1000, 200, 30, 4);
27     assert(x == 1234);
28 
29     status = sem_post(&done);
30     assert(status == 0);
31 
32     return arg;
33 }
34 
main(void)35 int main(void)
36 {
37     pthread_t th;
38     int i, status = sem_init(&done, 0, 0);
39     assert(status == 0);
40 
41     printf("starting\n");
42     fflush(stdout);
43     for (i = 0; i < 10; i++) {
44         status = pthread_create(&th, NULL, start_routine_2, NULL);
45         assert(status == 0);
46         status = pthread_create(&th, NULL, start_routine_3, NULL);
47         assert(status == 0);
48     }
49     for (i = 0; i < 20; i++) {
50         status = sem_wait(&done);
51         assert(status == 0);
52     }
53     printf("done\n");
54     fflush(stdout);   /* this is occasionally needed on Windows */
55     return 0;
56 }
57