#include #include #include #include #include #include #include #define NTHREAD 4 #define NTRY 50000 void * start(void *dummy) { int i; struct timespec ts0, ts1; printf("%d Starting\n", (int)getthrid()); clock_gettime(CLOCK_MONOTONIC, &ts0); for (i = 0; i < NTRY; i++) { clock_gettime(CLOCK_MONOTONIC, &ts1); if (timespeccmp(&ts0, &ts1, <=)) { ts0 = ts1; continue; } printf("%d Back %lld.%09lu => %lld.%09lu\n", (int)getthrid(), ts0.tv_sec, ts0.tv_nsec, ts1.tv_sec, ts1.tv_nsec); break; } printf("%d Stopped\n", (int)getthrid()); return (NULL); } int main(int argc, char *argv[]) { int i, n = NTHREAD; pthread_t *threads; threads = calloc(n, sizeof(pthread_t)); for (i = 0; i < n; i++) pthread_create(&threads[i], NULL, start, NULL); for (i = 0; i < n; i++) pthread_join(threads[i], NULL); }