Is `asprintf` thread-safe?

倾然丶 夕夏残阳落幕 提交于 2019-12-05 07:47:15

Yes, it's thread safe except when it reads the locale.

asprintf

Function: int asprintf (char **ptr, const char *template, …)
Preliminary: | MT-Safe locale | AS-Unsafe heap | AC-Unsafe mem

About the 'locale' exception, in particular:

Functions annotated with locale as an MT-Safety issue read from the locale object without any form of synchronization. Functions annotated with locale called concurrently with locale changes may behave in ways that do not correspond to any of the locales active during their execution, but an unpredictable mix thereof.

These kinds of functions are known as "conditionally" multithread safe because, in some contexts, they turn out not to be, so the programmer needs to take care of that.

glibc is free software and is probably the only (or the most important) library implementing asprintf.

So you can study (and even contribute to improve) its source code. See its stdio-common/asprintf.c & libio/vasprintf.c source files.

It looks like indeed, it is calling in a thread safe manner malloc and related things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!