Are functions from stdarg.h thread-safe?

Hi,
My program used to be single-thread, but now I want to add a thread for database I/O. The new thread utilizes some facilities from stdarg.h such as valist, va_start, va_end and vsprintf.
I fell it is too expensive to replace LIBC by LIBCMT, so I wonder if there is any threat of keeping LIBC for this case.
Does anyone know the implementation of vsprintf?
[371 byte] By [mamut] at [2007-11-19 7:19:50]
# 1 Re: Are functions from stdarg.h thread-safe?
stdarg.h is thread-safe.

Just take a look to this header file, the va_ API are implemented by macros.
va_list is a void *
va_start just get a pointer to the start of the stack.
and va_arg function access this pointer and increment it by the size of the second argument.

So these macros access parameters on stack like any parameter reference can do.

sprintf internally call vsprintf.
vsprintf is assumed to be thread-safe for multithreaded library.
I think that for all C-runtimes libraries, sprintf and vsprintf are thread-safe (think about how you should implement it).
But i am not sure that it is documented.

Moreover, if you use memory allocation function in more than one thread, you must serialize the calls by using a CriticalSection or a Mutex, because i am sure that generally memory allocations functions are not thread-safe in non thread-safe runtime libraries.

Note that GlobalAlloc is always thread-safe (and HeapAlloc is also thread-safe if the opposite is not specified).
SuperKoko at 2007-11-9 13:57:30 >
# 2 Re: Are functions from stdarg.h thread-safe?
I fell it is too expensive to replace LIBC by LIBCMT, so I wonder if there is any threat of keeping LIBC for this case.IMO, it's too expensive to take the risk that the functions you are calling in LIBC are thread safe. Why chance randoms errors in the code in the future because you assumed your usage is thread safe? What happens if down the road another programmer innocently decides to use another function in the library?

Arjay
Arjay at 2007-11-9 13:58:31 >