Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety of ::new in C++11

I am certain that, in practice, use of ::new is thread-safe. My question is what part of the standard provides that guarantee, if any? Is this a convention? Is this something where the standard gives implementations a lot of latitude (like the relatively loose constraints about what size each data type is) to support a wide variety of hardware?

I'm hoping that there's just a line in the C++11 standard somewhere that explicitly specifies "implementations of ::new must be thread-safe".

I'd also love to see some standardese about the thread-safety of operator new overloads. I imagine that they would also need to be required to be thread-safe, but these functions also do not fall under the blanket guarantee that const => thread safe (in C++11).

Thanks!

like image 554
Mark Avatar asked Oct 15 '14 06:10

Mark


People also ask

Is new thread-safe?

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.

Is std :: out thread-safe?

std::cout is already thread-safe.

Is std :: list size () thread-safe?

No, they're not thread-safe.

What is thread safety C++?

An object is thread-safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously. If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.


2 Answers

I believe this is implicitly guaranteed by the C++11 standard. If it were not, then usage of the operator new or new expression might cause a data race, and that would not be allowed by the standard. For reference, see §17.6.5.9 Data race avoidance and also

18.6.1.4 Data races [new.delete.dataraces]

"The library versions of operator new and operator delete, user replacement versions of global operator new and operator delete, and the C standard library functions calloc, malloc, realloc, and free shall not introduce data races (1.10) as a result of concurrent calls from different threads. 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 the next allocation (if any) in this order."

Your own overrides or your own replacements for the global operators should fulfill this requirement as well.

See also this proposal N3664 "Clarifying Memory Allocation", which puts more emphasis on that matter.

like image 167
CouchDeveloper Avatar answered Oct 04 '22 09:10

CouchDeveloper


The C++ standard does not outright require that new be thread-safe. Some implementations explicitly support building C++ code in single-threaded mode, where the C standard library, including malloc() may not be thread-safe. The platforms most of us use every day do offer thread-safe allocation, of course.

Even if your platform provides a thread-safe new, you still need to be wary if you use any libraries which implement their own operator new, or if you do so yourself. It's certainly possible to write a new which works only in a single thread--maybe even intentionally!

like image 20
John Zwinck Avatar answered Oct 04 '22 10:10

John Zwinck