Is new thread safe?

Started by
2 comments, last by Decept 17 years ago
I'm just wondering if the new operator in C++ is thread safe? I can't seem to find any clear information on that. I'm using Visual C++ 2005.
Advertisement
Yes.
Things to note:
The standard has no concept of threads. Thus any threaded code is inheritly NOT thread safe due to the fact that compilers are allowed to reorder operations, sometimes that reordering "could" result in code that is not thread safe. However, compilers typically have threading intrinsics that they recognize and hence will avoid reorderings in relation to those intrinsics. Neverless, due to the complete lack of awareness about threads, the standard does not provide that new, delete, or any operation be "safe" for threading. However, in your case (Visual Studio 2005), new uses HeapAlloc without the HEAP_NO_SERIALIZE flag, and hence is thread safe.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

If you're using a multi-threaded C++ runtime, it is. (See: Project Settings -> C++ -> Code Generation -> C++ runtime)

thanks for answering (man you guys are quick). Sorry it took me so long to get back to you.

That's good news that about new in VS2005. I have a follow up question regarding thread safety. I'm creating some code that uses winsock and I use the functions send() and recv(). Currently I use send() on the main thread and recv() in a separate thread. Is there any danger if those two functions run at the same time?

This topic is closed to new replies.

Advertisement