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