Skip to content

mi_heap_new() Is multithreading unsafe, as expected? #720

Closed as not planned

Description

@1027663760

windows - x86

heap = mi_heap_new()

Error prone code in multithreading:
address = mi_heap_malloc(heap, 666)
..................................
mi_free(address)

Code for safe operation without errors:
EnterCriticalSection()
address = mi_heap_malloc(heap, 666)
LeaveCriticalSection()
..................................
EnterCriticalSection()
mi_free(address)
LeaveCriticalSection()

Is this a bug or a design that meets expectations?

Activity

  1. daanx commented on Apr 7, 2023

    @daanx
    Collaborator

    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.

  2. 1027663760 commented on Apr 7, 2023

    @1027663760
    Author

    EnterCriticalSection()
    address = mi_heap_malloc(heap, 666)
    LeaveCriticalSection()
    ..................................
    EnterCriticalSection()
    mi_free(address)
    LeaveCriticalSection()

    So, that means writing code like this is multithreading safe:

    EnterCriticalSection()
    address = mi_ heap_ malloc(heap, 666)
    LeaveCriticalSection()

    ..................................
    mi_ free(address)

  3. 1027663760 commented on Apr 7, 2023

    @1027663760
    Author

    So does it also need to be written as follows:
    EnterCriticalSection()
    mi_heap_realloc(heap, address, 123)
    LeaveCriticalSection()

  4. daanx commented on Apr 14, 2023

    @daanx
    Collaborator

    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)

  5. 1027663760 commented on Apr 15, 2023

    @1027663760
    Author

    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

  6. jesup commented on Apr 26, 2023

    @jesup

    This issue makes it hard to swap in for code using jemalloc -- you can't substitute a mimalloc heap for a jemalloc arena.

  7. jesup commented on Apr 27, 2023

    @jesup

    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!

  8. 1027663760 commented on Apr 27, 2023

    @1027663760
    Author

    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!

    Just wait for the new version

  9. 1027663760 commented on Apr 7, 2024

    @1027663760
    Author

    Daan (@daanx) May I ask if you still recall this problem?😊

  10. liunyl commented on Apr 17, 2024

    @liunyl

    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.

  11. 1027663760 commented on Apr 23, 2024

    @1027663760
    Author
  12. daanx commented on Apr 23, 2024

    @daanx
    Collaborator

    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..

  13. anthonyyya commented on May 12, 2024

    @anthonyyya

    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.

  14. anthonyyya commented on May 14, 2024

    @anthonyyya

    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.

  15. robertblaketaylor commented on Mar 11, 2025

    @robertblaketaylor

    Any updates on this one?

  16. 1027663760 commented on Mar 21, 2025

    @1027663760
    Author

    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!

  17. github-actions commented on Aug 28, 2026

    @github-actions

    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!

  18. added
    stale
    on Aug 28, 2026
  19. github-actions commented on Sep 11, 2026

    @github-actions

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions