#include #include #include #include #include #include #define MAXTHREADS 16 static volatile int start = 0; static char *file = "/tmp"; static unsigned long count[MAXTHREADS][32]; void *start_routine(void *arg) { const char *filename; struct stat st; unsigned long *counter = arg; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); while (!start) /* nothing */; filename = file; for (;;) { stat(filename, &st); ++*counter; } } int main(int argc, char **argv) { pthread_t threads[MAXTHREADS]; unsigned long n; int i; if (argv[1]) file = argv[1]; for (i = 0; i < MAXTHREADS; i++) pthread_create(threads+i, NULL, start_routine, count[i]); start = 1; sleep(10); for (i = 0; i < MAXTHREADS; i++) pthread_cancel(threads[i]); for (i = 0; i < MAXTHREADS; i++) pthread_join(threads[i], NULL); n = 0; for (i = 0; i < MAXTHREADS; i++) n += count[i][0]; printf("Total loops: %lu\n", n); return 0; }