mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

What happens if thread_id gets reused?

Open malkia opened this issue 1 year ago • 2 comments

Scenario:

  • Segment stores the thread_id it was created in.
  • The thread is no longer available, but the segment stays still keeping it's number.
  • After really long time, the thread_id gets reused, and as it happens the current thread has this number and wants to free object from the segment above (with the same thread_id)?

Does mimalloc handle this case gracefully?

malkia avatar Oct 31 '24 21:10 malkia

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.

daanx avatar Nov 02 '24 13:11 daanx

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

malkia avatar Nov 03 '24 15:11 malkia