本文介绍了关于malloc和free线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
我听说的glibc的malloc()不是线程安全的,因为一个进程调用malloc的几个线程()同时会导致不确定的行为。我的问题是,如果一个线程调用free()的将另一个线程调用malloc(),这会导致不确定的行为呢?
I heard that glibc malloc() was not thread safe, since several threads of a process calling malloc() simultaneously will lead to undefined behaviour. And my question is if a thread calls free() will another thread is calling malloc(), will this lead to undefined behaviour as well?
推荐答案
如果您-pthreads链接,malloc()函数将线程glibc中。
If you link with -pthreads, malloc() will be threadsafe in glibc.
如果没有这些,链接器不会在一个线程的malloc,这将导致不确定的行为联系起来。
Without that, the linker doesn't link in a threadsafe malloc, which will lead to undefined behavior.
这篇关于关于malloc和free线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!