linux 和 gcc 4 中的 C++ 新运算符线程安全 [英] C++ new operator thread safety in linux and gcc 4
问题描述
很快我将开始研究使用共享内存的网格细化算法的并行版本.
Soon i'll start working on a parallel version of a mesh refinement algorithm using shared memory.
大学的一位教授指出,我们必须非常注意线程安全,因为编译器和 stl 都不是线程感知的.
A professor at the university pointed out that we have to be very careful about thread safety because neither the compiler nor the stl is thread aware.
我搜索了这个问题,答案取决于编译器(有些尝试有点线程感知)和平台(如果编译器使用的系统调用是线程安全的)).
I searched for this question and the answer depended on the compiler (some try to be somewhat thread-aware) and the plattform (if the system calls used by the compiler are thread-safe or not).
那么,在 linux 中,gcc 4 编译器为 new 运算符生成线程安全代码?
So, in linux, the gcc 4 compiler produces thread-safe code for the new operator?
如果没有,克服这个问题的最佳方法是什么?也许锁定对新运营商的每次调用?
If not, what is the best way to overcome this problem? Maybe lock each call to the new operator?
推荐答案
您将不得不寻找一个支持线程但没有线程安全的平台new
.事实上,new
(和malloc
)的线程安全性是它如此缓慢的原因之一.
You will have to look very hard to find a platform that supports threads but doesn't have a thread safe new
. In fact, the thread safety of new
(and malloc
) is one of the reasons it's so slow.
另一方面,如果您想要一个线程安全的 STL,您可以考虑 Intel TBB,它具有线程感知能力容器(尽管并非对它们的所有操作都是线程安全的).
If you want a thread safe STL on the other hand, you may consider Intel TBB which has thread aware containers (although not all operations on them are thread safe).
这篇关于linux 和 gcc 4 中的 C++ 新运算符线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!