Hello everyone,
I recently re-implemented the scalable_allocator from TBB for a research assignment at university, but was forbidden from looking at other allocator code as a requirement for the course. Now that the assignment is over and handed in, I have a quick question that is lingering from my own implementation ... (I'm very busy with exams now, so I hope someone has a few minutes who knows the code already to satisfy my curiosity).... and this issue was not described in the papers I read.
What happens when multiple threads request a new block from the OS? In particular, as I understand McRT-malloc and scalable_allocator, during initialization of a program (when I would expect a sudden burst of frequent memory requests) many blocks are going to be concurrently fetched from the OS itself. How is this handled by scalable_malloc in a concurrent manner? In our assignment, our OS allocator was not thread safe and so we had to find some way of ensuring mutual exclusion.
In my own implementation, I included an atomic int such that each thread that wanted to request a global block incremented this integer to signal their desire for a new block. Each thread would then attempt to enter the critical section, and the thread which did actually enter could make a large enough request to satisfy the block requirements for all waiting threads based on the value of this atomic int. The threads that did not gain access to the critical section would check frequently for new blocks (perhaps released by other threads), or attempt to enter the critical section themselves. The reasoning here is that threads could safely signal a thread that has entered the critical section that it would like a block, without actually interfering with mutual exclusion. I was quite proud of this optimization, and would love to hear comments from the experts.
There are some other lingering questions on things that I think are inefficient in the TBB allocator (as described in the papers) but I I really need to look at the code to check how it was handled there.
Thanks!