C ++新操作员线程安全在linux和gcc 4 [英] C++ new operator thread safety in linux and gcc 4

查看:226
本文介绍了C ++新操作员线程安全在linux和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编译器为新运算符生成线程安全代码?

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 (和 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,你可以考虑英特尔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).

这篇关于C ++新操作员线程安全在linux和gcc 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆