6

I am reading a book Efficient C++: Performance Programming Techniques Authors is saying following regarding global new and delete operators:

They manage memory in the process context, and since a process may spawn multiple threads, new() and delete() must be able to operate in a multithreaded environment. In addition, the size of memory requests may vary from one request to the next.

in Chapter 6. Single-Threaded Memory Pooling.

Is this true? I thought C++ does not have a notion of a Multi-threading environment, programmer need to handle is by using some means of mutual exclusion.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • 1
    And your question? How this would be implemented? – trojanfoe Nov 03 '11 at 07:34
  • @trojanfoe Book further provides implementation of memory allocation with single and multi thread supports, but when I tried his first approach it was failing on Visual Studio compiler, so I got a doubt. – Avinash Nov 03 '11 at 07:52

3 Answers3

7

It will depend on implementation. For example, Visual C++ runtime had both a single-threaded and a multithreaded version of heap in earlier version, but starting with Visual C++ 2005 it only has a multithreaded version. This MSDN article has a nice summary table.

When a multithreaded heap is used calls to memory allocation and deallocation are thread-safe at expense of additional overhead.

JFMR
  • 23,265
  • 4
  • 52
  • 76
sharptooth
  • 167,383
  • 100
  • 513
  • 979
3

C++ (C++03 standard) doesn't talk about multi-threading. However most of the platforms support thread-safe new/malloc. Here is a previous post discussing same kind of question.

In C++11, the threads are introduced.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 4
    +1 for the difference between the Standard and the practical applications. C and C++ have been multithreaded for a long time. – Matthieu M. Nov 03 '11 at 08:05
0

As of C++11 (which has the concept of a data race) the standard guarantees that new/delete, calloc/malloc/realloc/freewill occur in a single total order.

From n3690 18.6.1.4:

For purposes of determining the existence of data races, the library versions of operator new, user replacement versions of global operator new, the C standard library functions calloc and malloc, the library versions of operator delete, user replacement versions of operator delete, the C standard library function free, and the C standard library function realloc shall not introduce a data race (17.6.5.9). Calls to these functions that allocate or deallocate a particular unit of storage shall occur in a single total order, and each such deallocation call shall happen before (1.10) the next allocation (if any) in this order.

I could not find any such guarantees in previous versions of the standard, but (like others have said) I believe most implementations provide multi-threading support for memory allocation.

imreal
  • 10,178
  • 2
  • 32
  • 48