"is malloc thread-safe?" Code Answer


i read somewhere that if you compile with -pthread, malloc becomes thread safe. i´m pretty sure its implementation dependant though, since malloc is ansi c and threads are not.

if we are talking gcc:

compile and link with -pthread and malloc() will be thread-safe, on x86 and amd64.


another opinion, more insightful

{malloc, calloc, realloc, free, posix_memalign} of glibc-2.2+ are thread safe


By Jeff Handley on January 26 2022

Answers related to “is malloc thread-safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.