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