Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the C++ `free` function thread-safe?

That is, if my C++ application allocates memory in one thread using malloc, will free successfully de-allocate the memory, if called from another thread, or can I expect it to throw an exception? Both threads belong to the same process. I am using Visual Studio 2008. Thanks.

like image 719
Jim Fell Avatar asked Feb 25 '23 19:02

Jim Fell


1 Answers

The current standard doesn't make any guarantees about threads. On most implementations, malloc and free may be called from different threads. Visual C++ heap code also serializes access to heaps, so you should be fine.

like image 184
Adrian McCarthy Avatar answered Mar 07 '23 09:03

Adrian McCarthy