Does anyone know if free and malloc are threadsafe on visual C++ 2010?
I have having bizarre problems where memory is getting corrupted and im pretty much down to this being the only possiblity.
Does anyone know if the safety can be turned on and off and how?
Some functions are inherently thread-safe, for example memcpy() . Some functions, such as malloc() , can be made thread-safe by implementing the _mutex_ * functions.
By default, new/delete are often not thread safe in a FreeRTOS app because the basic malloc and free are not thread safe. Some embedded libraries have hooks that can be defined to make these functions thread safe.
Provided you're linking with thread-safe libraries and using the correct flags, yes, malloc should be thread-safe. If you think otherwise, you should post some code for us to examine. I should mention that, in the vast majority of cases, it's never the libraries (which have been tested by uncounted millions of people).
No, because you still call malloc multiple times (once per thread), and each call must return a distinct allocation.
Provided you're linking with thread-safe libraries and using the correct flags, yes, malloc
should be thread-safe.
If you think otherwise, you should post some code for us to examine.
I should mention that, in the vast majority of cases, it's never the libraries (which have been tested by uncounted millions of people). It's almost always your own code which, if you're thorough, has been tested by at most several dozen :-)
Not to say Microsoft (or any other large software house) doesn't ship bugs, just that they'd be discovered and fixed pretty damn quick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With