Discussion:
which functions can take over the standard ones in a multi-thread program?
(too old to reply)
Jack
2010-03-18 15:49:18 UTC
Permalink
Hi,
I hear about poeple say standard library functions are not usable in a
multi-thread program.
Such as printf....asctime, what mostly people do in order to tackle this
problem?
I mean to use which other functions are acceptable by C++?
Thanks
Jack
Ulrich Eckhardt
2010-03-19 07:44:40 UTC
Permalink
Post by Jack
I hear about poeple say standard library functions are not usable in a
multi-thread program.
Nonsense. However, neither C nor C++ standards mention threads at all, so
their behaviour in a multithreaded program are formally not defined.
Post by Jack
Such as printf....asctime, what mostly people do in order to tackle this
problem?
asctime() for example returns a pointer to a static buffer, which means its
content may be overwritten by a different thread. There are thread-safe
versions around using a thread-local buffer, alternatively there is
asctime_r() for example. In any case, those deficiencies are documented
including suggested alternatives. Just search the web.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Martin B.
2010-03-19 12:20:06 UTC
Permalink
Post by Ulrich Eckhardt
Post by Jack
I hear about poeple say standard library functions are not usable in a
multi-thread program.
Nonsense. However, neither C nor C++ standards mention threads at all, so
their behaviour in a multithreaded program are formally not defined.
Post by Jack
Such as printf....asctime, what mostly people do in order to tackle this
problem?
asctime() for example returns a pointer to a static buffer, which means its
content may be overwritten by a different thread. There are thread-safe
versions around using a thread-local buffer, ...
AFAIK, with Visual Studio linking with (the default) Multi-threaded
Runtime Library will have all std-c library function thread-safe.

br,
Martin

Loading...