10

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.

Community
  • 1
  • 1
xiaq
  • 764
  • 1
  • 6
  • 13
  • Which version of the standard are you working to? – David Heffernan Dec 30 '12 at 12:16
  • 1
    I think you are confusing reentrancy with thread safety. A function can be thread-safe but not reentrant. – interjay Dec 30 '12 at 12:16
  • @interjay read the background question for why I insist on reentrancy, not just thread-safety. – xiaq Dec 30 '12 at 12:20
  • I don't know who can say something definitive about malloc, if I was you I would higly suggest to read something from the standard about malloc, malloc doesn't even grant some memory to be really allocated, if you want "safe code" you probably want to avoid malloc because malloc it's not defined and it's implementation dependant so its behaviour is platform-specific . – user1824407 Dec 30 '12 at 12:41

3 Answers3

3

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.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
3

Standard allows new to be just a wrapper around malloc, so if malloc can be not reentrant, so can new.

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
  • While this is the case for almost all implementations, a minor nitpick is that the standard doesn't say `new` **must** use `malloc`. So it may or maynot hold true. – Alok Save Dec 30 '12 at 12:57
  • @AlokSave added emphasis on _allows_ ;) – milleniumbug Dec 30 '12 at 13:15
  • @AlokSave in [section 16.4.6.9](https://timsong-cpp.github.io/cppwp/reentrancy) the C++ standard defines that "Except where explicitly specified in this document, it is implementation-defined which functions in the C++ standard library may be recursively reentered". That is part of the library part of the standard, but as long as the [basic part of the standard](https://timsong-cpp.github.io/cppwp/basic.stc.dynamic.allocation) does not explicitly state something I would assume that new is not reentrant. – Sonic78 Oct 31 '20 at 09:46
1

Thread-safety and re-entrance are not exactly the same.

AFAIK, the C++ ISO standard does not guarantee thread-safety for new and delete operators. But g++ implementation does provide thread-safetly (and it's one of the reasons it's slow).

Swapnil
  • 8,201
  • 4
  • 38
  • 57
  • 2
    The question is about reentrancy. :) Read the background question for why I insist on that. – xiaq Dec 30 '12 at 12:20