Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc / new lock and multithreading

How should I use new in a multithread environment?

Precisely: I have a piece of code that I run with 40 threads. Each thread invokes new a few times. I noticed that performance drops, probably because threads lock in new (significant time is spent in __lll_lock_wait_parallel and __lll_unlock_wait_parallel). What is the best alternative to new / delete I can use?

Related:

  • http://dsc.sun.com/solaris/articles/multiproc/multiproc.html

  • Is malloc thread-safe?

like image 762
Jakub M. Avatar asked May 01 '26 09:05

Jakub M.


1 Answers

Even if you are using the new operator, its using malloc underneath to do the allocation and deallocation. The focus should be on the allocator and not the API used to reach it in these circumstances.

TCMalloc is a malloc created at Google specifically for good performance in a multi-threading environment. It is part of google-perf-tools.

Another malloc you might look at is Hoard. It has much the same aims as TCMalloc.

like image 151
Will Avatar answered May 03 '26 00:05

Will



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!