Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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).

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?

like image 353
Gaston Avatar asked Apr 28 '09 03:04

Gaston


2 Answers

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.

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).

like image 100
Max Lybbert Avatar answered Sep 23 '22 13:09

Max Lybbert


Generally the new operator is thread safe - however thread safety guarantees for calls into the STL and the standard library are governed by the standard - this doesn't mean that they are thread unaware - they tend to have very well defined guarantees of thread safety for certain operations. For example iterating through a list in a read-only fashion is thread safe for multiple readers, while iterating through a list and making updates is not. You have to read the documentation and see what the various guarantees are, although they aren't that onerous and they tend to make sense.

like image 34
1800 INFORMATION Avatar answered Sep 19 '22 13:09

1800 INFORMATION