You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mi_heap_new() Is multithreading unsafe, as expected? #720
Ah yes -- you can only allocate in a heap that created in your thread (free-ing can be from any thread). It would be great to lift this restriction but it is not entirely trivial to do as the design of mimalloc generally assumes that heaps are always thread local.
So, for now, just thread local allocation in explicit heaps.
Ah, no, it is more limited even -- you can only allocate in a heap that was created in the same thread. Locking access is not enough. This is because the thread id is used to check internally to associate segments etc. Now, this can be addressed, we could have a special thread id for multithread safe heaps and add the correct checks at the right points. It is a bit of work though as that needs to be done carefully (-- it is on the todo list)
Ah, no, it is more limited even -- you can only allocate in a heap that was created in the same thread. Locking access is not enough. This is because the thread id is used to check internally to associate segments etc. Now, this can be addressed, we could have a special thread id for multithread safe heaps and add the correct checks at the right points. It is a bit of work though as that needs to be done carefully (-- it is on the todo list)
Looking forward to the new version, the programming language I am using can only compile 32-bit programs, so memory space is relatively scarce
Ah, no, it is more limited even -- you can only allocate in a heap that was created in the same thread. Locking access is not enough. This is because the thread id is used to check internally to associate segments etc. Now, this can be addressed, we could have a special thread id for multithread safe heaps and add the correct checks at the right points. It is a bit of work though as that needs to be done carefully (-- it is on the todo list)
What would be involved in making multithread-safe heaps? Does a design exist? (or a design in someone's head)? Roughly what work would need to be done? Thanks!
Ah, no, it is more limited even -- you can only allocate in a heap that was created in the same thread. Locking access is not enough. This is because the thread id is used to check internally to associate segments etc. Now, this can be addressed, we could have a special thread id for multithread safe heaps and add the correct checks at the right points. It is a bit of work though as that needs to be done carefully (-- it is on the todo list)
What would be involved in making multithread-safe heaps? Does a design exist? (or a design in someone's head)? Roughly what work would need to be done? Thanks!
Daan (@daanx) May I ask if you still recall this problem?😊
I've implemented a mi_override_thread_id in our own mimalloc private patch and multi threading alloc with the same heaps works fine in our project.
// thread 1
heap = mi_heap_new();
// get the internal thread id in mimalloc
heap_thread = mi_get_thread_id();
// Other threads
EnterCriticalSection()
mi_override_thread_id(heap_thread);
mi_heap_alloc(heap);
mi_restore_thread_id();
LeaveCriticalSection()
override_thread_id basically sets a thread local bool in mimalloc which makes its internal mi_thread_id() returns the given value instead of its own thread id.
well, hello Ling (@1027663760) :-) multi-thread safe heaps are not yet included in the latest release but I will try to have it in the next one (coming soonish, maybe next month). Your approach above kind-of works but I think I have a way to do this more efficiently using biased locking. to be continued..
Daan (@daanx) would there be a release in the dev branch soon for this? I am going to fork mimalloc with liunyl 's changes in the meantime, as I am blocked on this.
I tried out a fork with liunyl's attempt, but I'm actually seeing segfaults on allocation on _mi_page_malloc. Daan (@daanx) , is there any other changes you think are required beyond this for getting it to work? anthonyyya@c50877d
Daan (@daanx) , Hi there! 😊 I just saw the dev3 introduction and wanted to ask - is the thread-safe heap functionality going to be implemented in dev3? Thanks!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in the next 14 days. Thank you for your contributions!
This issue was closed because it has been inactive for 14 days since being marked as stale. Please feel free to reopen if this is still an active issue.
Activity
daanx commentedon Apr 7, 2023
Ah yes -- you can only allocate in a heap that created in your thread (free-ing can be from any thread). It would be great to lift this restriction but it is not entirely trivial to do as the design of mimalloc generally assumes that heaps are always thread local.
So, for now, just thread local allocation in explicit heaps.
1027663760 commentedon Apr 7, 2023
So, that means writing code like this is multithreading safe:
EnterCriticalSection()
address = mi_ heap_ malloc(heap, 666)
LeaveCriticalSection()
..................................
mi_ free(address)
1027663760 commentedon Apr 7, 2023
So does it also need to be written as follows:
EnterCriticalSection()
mi_heap_realloc(heap, address, 123)
LeaveCriticalSection()
daanx commentedon Apr 14, 2023
Ah, no, it is more limited even -- you can only allocate in a heap that was created in the same thread. Locking access is not enough. This is because the thread id is used to check internally to associate segments etc. Now, this can be addressed, we could have a special thread id for multithread safe heaps and add the correct checks at the right points. It is a bit of work though as that needs to be done carefully (-- it is on the todo list)
1027663760 commentedon Apr 15, 2023
Looking forward to the new version, the programming language I am using can only compile 32-bit programs, so memory space is relatively scarce
jesup commentedon Apr 26, 2023
This issue makes it hard to swap in for code using jemalloc -- you can't substitute a mimalloc heap for a jemalloc arena.
jesup commentedon Apr 27, 2023
What would be involved in making multithread-safe heaps? Does a design exist? (or a design in someone's head)? Roughly what work would need to be done? Thanks!
1027663760 commentedon Apr 27, 2023
Just wait for the new version
1027663760 commentedon Apr 7, 2024
Daan (@daanx) May I ask if you still recall this problem?😊
liunyl commentedon Apr 17, 2024
I've implemented a mi_override_thread_id in our own mimalloc private patch and multi threading alloc with the same heaps works fine in our project.
override_thread_id basically sets a thread local bool in mimalloc which makes its internal mi_thread_id() returns the given value instead of its own thread id.
1027663760 commentedon Apr 23, 2024
Daan (@daanx) hello
daanx commentedon Apr 23, 2024
well, hello Ling (@1027663760) :-) multi-thread safe heaps are not yet included in the latest release but I will try to have it in the next one (coming soonish, maybe next month). Your approach above kind-of works but I think I have a way to do this more efficiently using biased locking. to be continued..
anthonyyya commentedon May 12, 2024
Daan (@daanx) would there be a release in the dev branch soon for this? I am going to fork mimalloc with liunyl 's changes in the meantime, as I am blocked on this.
anthonyyya commentedon May 14, 2024
I tried out a fork with liunyl's attempt, but I'm actually seeing segfaults on allocation on
_mi_page_malloc. Daan (@daanx) , is there any other changes you think are required beyond this for getting it to work?anthonyyya@c50877d
I don't mind testing out the rough idea.
robertblaketaylor commentedon Mar 11, 2025
Any updates on this one?
1027663760 commentedon Mar 21, 2025
Daan (@daanx) , Hi there! 😊 I just saw the dev3 introduction and wanted to ask - is the thread-safe heap functionality going to be implemented in dev3? Thanks!
github-actions commentedon Aug 28, 2026
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in the next 14 days. Thank you for your contributions!
github-actions commentedon Sep 11, 2026
This issue was closed because it has been inactive for 14 days since being marked as stale. Please feel free to reopen if this is still an active issue.