title: Threadsafe memory manager: Data race in 320 MB buffer?
body: <p>I am working on a multithreaded VC++ (windows) program that performs some continuous simulation in the main thread. This simulation uses objects that get created in the same thread or in a loader-thread (both can be the case). The same is true for deleting.</p>
  
  <p>I did this so the main thread would not block while very large objects (made up of <em>a lot</em> of other objects) get loaded and unloaded dynamically. This basically worked. But (for example) while the unloader thread is deleting one object after another continuously the new and delete calls of other threads take forever. This seems to be caused by some internal thread protection.</p>
  
  <p>My next move was to reduce the number of new/delete calls by pooling those many smaller objects. So I wrote a primitive memory manager. The manager allocates a 320 MB buffer using malloc() and supplies allocation functionality to the other parts of my program using blocks of 64 bytes. Now I use placement new to create the smaller objects. The manager's alloc/free methods are entirely mutex(lock) protected.</p>
  
  <p>What happens now is that it simply does not work correctly in a multithreading environment. Modifications of the custom managed objects get ignored and so on. I assume what happens is that the entire buffer is viewed as a single variable and as such must not have read/write data races. Is this the case?</p>
  
  <p>Restricting access to one object while an entirely different one gets created/deleted is not an option as you can imagine. So what's the way to go in a situation like this?</p>