Using C++, when I create a new object, then 'new' keyword do some locks (since it does some memory allocation, etc). So, I cannot use it directly in a lock-free software.
However, does the 'delete' keyword create any kind of lock?
I'm still using C++98, I think it will work in the same way in C++11;
The reason for locks in new
is, as you say, the memory allocation. If the allocation requires a lock, it is very likely, at least under some circumstances, that the corresponding de-allocation also requires locks. It is of course possible that the de-allocation is lockless, even if allocation requires locks.
Note that delete
itself doesn't do locking (neither does new
), it is the underlying primitive that does. So if you have a lock-less allocator, then there should be no issues using new
and delete
. [Presuming the constructor and destructor are lock-less, of course].
Note that placement forms new
and delete
shouldn't be locking.
Avoiding new
and delete
(or at least allocations) probably also restricts a huge amount of what you can do. All containers except std::array
(in C++11) will perform allocations, for example, so no std::vector
, std::string
, std::map
, etc. Many constructors perform allocations for the object created. And of course, there may be locks in other functions than new
and delete
- I/O functions often have locks to prevent completely garbled output (but not necessarily guaranteeing the whole output to NOT be garbled, either). Pretty much anything that uses a common shared resource is likely to have locks at some level, be that inside the OS or at the user-mode level.
The C++ standard doesn't really say anything in this regard, it is an implementation detail.