12 void* tester(void *arg
)
14 struct dirent en
, *enp
;
15 DIR *d
= opendir("/");
19 perror("opendir() failed");
23 pthread_barrier_wait(&bar
);
26 r
= readdir_r(d
, &en
, &enp
);
29 perror("thread tester error");
33 } while (enp
!= NULL
);
38 void* checker(void *arg
)
40 pthread_barrier_wait(&bar
);
41 printf("Passed barrier!\n");
45 int main(int argc
, char *argv
[])
53 printf("Need one argument\n");
57 nthreads
= atoi(argv
[1]);
58 threads
= malloc(sizeof(pthread_t
) * (nthreads
+ 1));
59 if (threads
== NULL
) {
60 fprintf(stderr
, "malloc failed!\n");
64 printf("Trying with %d threads\n", nthreads
);
66 if ( (ret
= pthread_barrier_init(&bar
, NULL
, nthreads
+ 1)) != 0)
68 printf("error occurred during pthread_barrier_init, ret = %d\n", ret
);
73 printf("Creating checker thread\n");
74 pthread_create(&th
, NULL
, checker
, NULL
);
76 printf("Creating tester threads\n");
79 for (i
= 0; i
< nthreads
; i
++) {
80 ret
= pthread_create(&threads
[i
], NULL
, tester
, NULL
);
87 printf("Starting join\n");
88 for (i
= 0; i
< nthreads
; i
++) {
90 pthread_join(threads
[i
], NULL
);
93 printf("All done!\n");