There are at least three concepts here, all of which are conflated in colloquial language, which might be why you were confused.
- thread-safe
- critical section
- re-entrant
To take the easiest one first: Both malloc
and printf
are thread-safe. They have been guaranteed to be thread-safe in Standard C since 2011, in POSIX since 2001, and in practice since long before that. What this means is that the following program is guaranteed not to crash or exhibit bad behavior:
#include <pthread.h>
#include <stdio.h>
void *printme(void *msg) {
while (1)
printf("%s\r", (char*)msg);
}
int main() {
pthread_t thr;
pthread_create(&thr, NULL, printme, "hello");
pthread_create(&thr, NULL, printme, "goodbye");
pthread_join(thr, NULL);
}
An example of a function which is not thread-safe is strtok
. If you call strtok
from two different threads simultaneously, the result is undefined behavior — because strtok
internally uses a static buffer to keep track of its state. glibc adds strtok_r
to fix this problem, and C11 added the same thing (but optionally and under a different name, because Not Invented Here) as strtok_s
.
Okay, but doesn't printf
use global resources to build its output, too? In fact, what would it even mean to print to stdout from two threads simultaneously? That brings us to the next topic. Obviously printf
is going to be a critical section in any program that uses it. Only one thread of execution is allowed to be inside the critical section at once.
At least in POSIX-compliant systems, this is achieved by having printf
begin with a call to flockfile(stdout)
and end with a call to funlockfile(stdout)
, which is basically like taking a global mutex associated with stdout.
However, each distinct FILE
in the program is allowed to have its own mutex. This means that one thread can call fprintf(f1,...)
at the same time that a second thread is in the middle of a call to fprintf(f2,...)
. There's no race condition here. (Whether your libc actually runs those two calls in parallel is a QoI issue. I don't actually know what glibc does.)
Similarly, malloc
is unlikely to be a critical section in any modern system, because modern systems are smart enough to keep one pool of memory for each thread in the system, rather than having all N threads fight over a single pool. (The sbrk
system call will still probably be a critical section, but malloc
spends very little of its time in sbrk
. Or mmap
, or whatever the cool kids are using these days.)
Okay, so what does re-entrancy actually mean? Basically, it means that the function can safely be called recursively — the current invocation is "put on hold" while a second invocation runs, and then the first invocation is still able to "pick up where it left off." (Technically this might not be due to a recursive call: the first invocation might be in Thread A, which gets interrupted in the middle by Thread B, which makes the second invocation. But that scenario is just a special case of thread-safety, so we can forget about it in this paragraph.)
Neither printf
nor malloc
can possibly be called recursively by a single thread, because they are leaf functions (they don't call themselves nor call out to any user-controlled code that could possibly make a recursive call). And, as we saw above, they've been thread-safe against *multi-*threaded re-entrant calls since 2001 (by using locks).
So, whoever told you that printf
and malloc
were non-reentrant was wrong; what they meant to say was probably that both of them have the potential to be critical sections in your program — bottlenecks where only one thread can get through at a time.
Pedantic note: glibc does provide an extension by which printf
can be made to call arbitrary user code, including re-calling itself. This is perfectly safe in all its permutations — at least as far as thread-safety is concerned. (Obviously it opens the door to absolutely insane format-string vulnerabilities.) There are two variants: register_printf_function
(which is documented and reasonably sane, but officially "deprecated") and register_printf_specifier
(which is almost identical except for one extra undocumented parameter and a total lack of user-facing documentation). I wouldn't recommend either of them, and mention them here merely as an interesting aside.
#include <stdio.h>
#include <printf.h> // glibc extension
int widget(FILE *fp, const struct printf_info *info, const void *const *args) {
static int count = 5;
int w = *((const int *) args[0]);
printf("boo!"); // direct recursive call
return fprintf(fp, --count ? "<%W>" : "<%d>", w); // indirect recursive call
}
int widget_arginfo(const struct printf_info *info, size_t n, int *argtypes) {
argtypes[0] = PA_INT;
return 1;
}
int main() {
register_printf_function('W', widget, widget_arginfo);
printf("|%W|\n", 42);
}
malloc
andprintf
as reentrant functions, and this is for reason. In this quesiton, the OP wanted to know what the reason is. – Carrickmalloc
has to be reentrant, and so the glibc developers can do so and still claim to be developing "a UNIX system". – Coplanarmalloc
. If so, thenmalloc
will be marked non-reentrant, regardless of whether that implementation strategy is "canonical" or even common. – Coplanarmalloc
, almost any implementation would be expected to be non-reentrant, so the job of the standards authors was very easy. – Coplanar