malloc 线程安全吗? [英] Is malloc thread-safe?
本文介绍了malloc 线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
malloc()
函数是否可重入?
推荐答案
我在某处读到,如果您使用 -pthread 进行编译,malloc 将成为线程安全的.我很确定它的实现依赖,因为 malloc 是 ANSI C 而线程不是.
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.
如果我们在谈论 gcc:
If we are talking gcc:
编译和链接 -pthread 和malloc() 将是线程安全的,在 x86 上和 AMD64.
Compile and link with -pthread and malloc() will be thread-safe, on x86 and AMD64.
另一种观点,更有见地
{malloc, calloc, realloc, free,glibc-2.2+ 的 posix_memalign} 是线程安全
{malloc, calloc, realloc, free, posix_memalign} of glibc-2.2+ are thread safe
http://linux.derkeiler.com/Newsgroups/comp.os.linux.development.apps/2005-07/0323.html
这篇关于malloc 线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文