Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any thread-unsafe mallocs?

Tags:

c++

c

I need a thread-unsafe, lockless malloc to use it in a single threaded application. I expect to gain a little performance boost because of this. I need it on win and linux, and it must guaranteed to be single-threaded. Any ideas?

like image 226
Iurii Avatar asked Dec 12 '25 23:12

Iurii


2 Answers

Have you actually profiled your code and shown that it's spending a significant portion of its time in malloc? I would say instead of a replacement malloc, first consider an alternate algorithm that doesn't do as much dynamic allocation. If that's not an option I would consider a memory pool approach (they can be very fast with fixed-size blocks).

like image 124
Mark B Avatar answered Dec 14 '25 13:12

Mark B


To answer the question, some implementations have compiler options to remove the thread safety from malloc. This answer: Is malloc thread-safe? suggests that NO_THREADS can be defined to remove the thread-safety from malloc, but I have not tested this.

You should listen to the other posts, however, it is unlikely to be the best place to start your optimisiations.

like image 40
Tom Avatar answered Dec 14 '25 14:12

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!