/***** * * Test program that evokes 'localtime'+threads bug * * Build: gcc -o loctim_bug loctim_bug.c * Run: ./loctim_bug.exe * *****/ #include #include #define MAX_THR 25 #define MAX_LOOP 10000 /* Set this to 24 minus your non-DST timezone */ /* For example, EDT = -5, therefore use 19 */ #define EXPECTED 19 void * time_loop(void* data) { int arg = *((int *)data); time_t when = (time_t)arg; int ii; struct tm *tm; struct timespec sleep, remainder; sleep.tv_sec = 0; sleep.tv_nsec = 1000; for (ii=0; ii < MAX_LOOP; ii++) { tm = localtime(&when); if (tm->tm_hour != EXPECTED) printf("BUG in thread %2d: expected: %d got: %d\n", arg, EXPECTED, tm->tm_hour); nanosleep(&sleep, &remainder); } } int main(int argc, char **argv) { int ii; pthread_t p_thread[MAX_THR]; int args[MAX_THR+2]; /* Launch threads */ for (ii=0; ii < 50; ii++) { args[ii] = ii; pthread_create(&p_thread[ii], NULL, time_loop, (void*)&args[ii]); } /* Make sure threads finish */ args[MAX_THR] = MAX_THR; time_loop((void*)&args[MAX_THR]); args[MAX_THR+1] = MAX_THR+1; time_loop((void*)&args[MAX_THR+1]); printf("Done\n"); } /* EOF */