I'd recommend reading a good book, preferrably about multithreading, or you
could try searching the fine web for information.
--
Arnold Hendriks <a.hen...@b-lex.com>
B-Lex Information Technologies, http://www.b-lex.com/
Did you search on google? If not, do that right now.
A function is thread-safe if it can be called by different threads and
still "work." A function that makes use of kernel or library static
buffers are generally not thread-safe, for example, gethostbyname().
It uses its own buffer, so if two threads call gethostbyname() at the
same time, the second call will overwrite the data created by the first
call, and both threads will have the same information, when in fact they
shouldn't. Make sense?
--
J o s h u a J o n e s *** email: jajones(at)cc.gatech.edu
__ .~.
College of Computing at the | / / _ _ _ _ _ __ __ /V\
Georgia Institue of Technology | / /__ / / / \// //_// \ \/ / // \\
Atlanta, Georgia, U.S. | /____/ /_/ /_/\/ /___/ /_/\_\ /( )\
*Debian GNU/Linux* ^^-^^
Yeah... the only question is: what does "work" actually mean... ;-)
"corruption" of internally shared data -- violation of 4.10 rules
aside! For example:
http://groups.google.com/groups?selm=3CCE560A.6640EFAB%40web.de
(Subject: Re: is fprintf threadsafe?)
regards,
alexander.
< "P.S." just managed to escape... ;-) >
P.S. Consider:
OPINION "A":
http://groups.google.com/groups?selm=slrn897h89.o2r.kaz%40ashi.FootPrints.net
"....
Also, you must lock the FILE * stream for exclusive access, for
example:
flockfile(stdout);
printf("this can be printed by one thread at a time\n");
funlockfile(stdout);
without this locking, concurrent calls to printf for the same stream
cause
undefined behavior.
...."
(well, I guess, the behavior is actually NOT "undefined"... just/but):
http://groups.google.com/groups?selm=363486C2.D6AF1AA6%40ms.com
"....
Hmmm. I do get interleaved output on Solaris 2.5.1 which made me
replace printf with sprintf+puts"
OPINION "B":
http://groups.google.com/groups?selm=36347DE1.8EF998F1%40zko.dec.com
"....
For POSIX conformance, printf() must lock the process' stdio file
stream.
That is, the output is "atomic". Thus, if two threads both call a
single
printf() simultaneously, each output must be correct. E.g., for
printf ("%d, %d\n", 1, 2); printf ("%s, %s"\n", "abc",
"def");
you might get
1, 2
abc, def
or you might get
abc, def
1, 2
but no more "bizarre" variations. If you do, then the implementation
you're using is broken.
...."
and, also ("likewise"):
http://groups.google.com/groups?selm=36A5EB10.4A888BAE%40zko.dec.com
"....
Efficiency/redundancy: All of your flockfile/funlock file calls are
unnecessary. They won't hurt, but you only need them when you want to
use
multiple output calls and ensure that the sequence of outputs cannot be
separated by the action of some other thread. E.g.,
flockfile(stderr);
fprintf(stderr,"my_errno %x", my_errno);
fprintf(stderr," for id = %d\n", (int) pthread_self());
funlockfile(stderr);
...."