threads and printf problem
Ferenc Engard
ferenc@engard.hu
Thu Apr 24 14:36:00 GMT 2003
Hi all,
I am learning linuxthreads, and have the following problem:
I start a hundred of threads with the following code:
void *task(void *arg) {
int i=rand()%100000;
printf("%4d start (sleep: %5d)\n",(int)arg,i);
usleep(i);
printf("%4d stop\n",(int)arg);
return 0;
}
The problem is, that in the output there are several duplicate rows:
[...]
63 start (sleep: 36327)
64 start (sleep: 60336)
64 start (sleep: 60336)
3 stop
5 stop
[...]
The 64th thread's start row is duplicated! The duplication is occures
when a 'start' row is followed by a 'stop' row. It is quite frequent,
but if I redirect the output to a file (./test >filename) then it does
not occure.
Moreover, after many-many tries here is one more:
[...]
95 start (sleep: 10012)
96 7 stop <-- 96 start... 1st half
12 stop
[...]
72 stop
start (sleep: 36226) <-- 96 start... 2nd half
97 start (sleep: 98586)
[...]
I thought that glibc (including printf) is thread-safe, but it
seems is not. My question is, how can I determine which functions are
thread-safe and which are not?
glibc 2.3.1, linux 2.4.20
Thanks:
Circum
More information about the Libc-alpha
mailing list