Closed liunyl closed 4 months ago
Also is it safe to call mi_heap_visit_blocks
from other threads that do not own the heap? It is not particularly mentioned in the doc but it seems like this func just loops over the pages in heap and calls the passed in visit func, so the page in this heap cannot be freed during the time this function is called.
Is it possible to allocate from a heap from different threads if we can make sure that only one thread can do the allocation at a time?
No, I don't think so. I am certainly not an expert on mimalloc, but as I am trying to wrap my head around multi-threading limitations, I understand that this is not possible because the heap uses the current thread id internally (apart from usually being a thread local to begin with). Using a heap from a different thread than the one it was created will have mismatching thread ids.
Is it possible to allocate from a heap from different threads if we can make sure that only one thread can do the allocation at a time?
No, I don't think so. I am certainly not an expert on mimalloc, but as I am trying to wrap my head around multi-threading limitations, I understand that this is not possible because the heap uses the current thread id internally (apart from usually being a thread local to begin with). Using a heap from a different thread than the one it was created will have mismatching thread ids.
We've applied a private patch on mimalloc to add a function that override thread default heap and thread id internally. Right now it's working just fine as long as we make sure that one heap can only be accessed by one thread at a time.
Is it possible to allocate from a heap from different threads if we can make sure that only one thread can do the allocation at a time?
We have seen from the doc that
Our project divides memory into shards and we want to assign a heap for each shard. The shard can be visited by multiple physical threads, but we have made sure that only one thread can visit the shard at a time. Also each physical thread can only visit one single shard. In this case is it safe to use the same heap for different threads? If so we can override the _mi_prim_thread_id and return a shard id with it, will this cause other issues with mimalloc?