• Advertisement
Sign in to follow this  

Is new thread safe?

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement