microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.61k stars 866 forks source link

What happens if thread_id gets reused? #956

Open malkia opened 3 weeks ago

malkia commented 3 weeks ago

Scenario:

Does mimalloc handle this case gracefully?

daanx commented 2 weeks ago

Hi @malkia -- ah, no worries -- this is handled well. In particular, when a thread terminates the _mi_thread_done is called which will "abandon" all segments and set their thread-id to 0 (and preventing reused thread-id issues). Btw. we need to keep the segments alive since they contain still live data resulting from pointers that are shared with other threads). Later, other threads can "reclaim" such abandoned segments (see _mi_segment_try_reclaim) and reuse the memory that was abandoned.

malkia commented 2 weeks ago

Awesome - thank you so much! This makes it for another re-reading of the source code! Much appreciated!