You are reacting, not thinking. Consider the effort and runtime
needed to make a malloc system thread-safe. You have to provide
something that prevents thread-changing, and something that
restores that ability.
This is called a mutex. You should learn about them in any course in
operating systems or multitasking.
[...] Now you have to ensure that every malloc
(etc) call is preceded by the preventer, and followed by the
restorer. Don't forget the various error paths.
There are many classes of applications that need to be programmed
correctly. And yes, the std library is one of them. What is your
point? Its not actually even that hard to get it right.
[...] This may easily
slow down malloc operation by a factor of two.
No. A correctly implemented mutex should cost very little in the
ideal case, when there is no contention.
[...] Those
prevent/restore calls are system calls, and expensive.
You are assuming that they reach the OS every time. Look up the x86
instruction: CMPXCHG8B. Different CPUs do it differently, but
essentially boil down to that sort of functionality. You only enter
the OS to *block* when there is contention.
[...] They
probably need some level of privilege. Most malloc operation is
purely local.
WTF? You need exactly the same privileges that gave you access to the
dynamic memory in the first place.
You can replace all that garbage by a simple discipline. Only call
malloc (etc) from one thread. The results are not system
dependant.
There are certain tell tale signs of when a programmer has basically
gone to pasture. When they can no longer embrace leading edge
technologies, or understand modern concepts. They used to be called
COBOL programmers.
Every university with a decent comp sci course have augmented their
course offerings with multi-processing content. Tell these students
they can only call malloc from one of the threads and they will laugh
in your face.