Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C++'s new operator reentrant (or async-safe)?

Tags:

c++

reentrancy

The background is in this question of mine. Put shortly, I have to fork in a multithreaded C++ program, so I'd like to figure out how much I can do when restricted to reentrant functions only, and one of the most essential things is dynamic memory.

So, malloc is known to be non-reentrant. But what about C++'s new? I googled for that with not many relevant results (mostly due to the difficulty to hit the correct "new"), but there is at least one claim that new is reentrant. There is also a relevant question concerning the whole C++ standard library with no satisfying answer.

Edit: I guess the standard didn't say anything about this, so I'm mostly concerned about major implementations.

like image 263
xiaq Avatar asked Dec 30 '12 12:12

xiaq


People also ask

Is printf reentrant and thread safe?

malloc and printf usually use global structures, and employ lock-based synchronization internally. That's why they're not reentrant. The malloc function could either be thread-safe or thread-unsafe.

What is reentrant in C?

Function is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.


1 Answers

I've looked at both the gcc libsupc++ and clang libc++ source, for replacing the standard-conforming C++ new/delete operators - to support native SIMD alignment requirements on platforms where it wasn't guaranteed by malloc.

They are basically wrappers for malloc and free with some EH logic, etc. I am not a language lawyer, but unless both have it wrong, I think it's safe to conclude: no, they are not reentrant.

like image 187
Brett Hale Avatar answered Oct 21 '22 03:10

Brett Hale