8be Subj : Re: speed-up critical regions To : comp.programming.threads From : David Schwartz Date : Thu Jun 16 2005 09:56 am wrote in message news:1118914132.360340.17740@g49g2000cwa.googlegroups.com... >> > An object could be visible over multiple threads. So, while Thread A >> > may ask for a new object, Thread B may ask for destroying an object was >> > created by Thread A. Both are acting on the internal structure of the >> > same pool (owned by Thread A), so you need to lock the alloc function >> > against the destroy function. >> >> Just return the object to thread B's allocator. You may need to >> balance >> the allocators though, which is kind of a PITA. > Sorry, but I don't understand your point of view. Would you explain > more? Sure. If thread A allocates an object, get it from thread A's allocator. If it's later freed by thread B, that's fine, just return the object to thread B's allocator. The only problem is that if this happens an awful lot, there will end up being lots of free objects in thread B's pool and thread A will have to keep allocating more memory. So you may need to 'rebalance' the allocators periodically, taking memory from threads that have huge free pools and giving it to threads that don't. The advantage of this is that when you're not rebalancing, the threads don't block each other at all. Each thread uses its own allocator and never contends for access to it. However, from time to time, you may have to lock another thread's allocator to rebalance with it. If you rebalance often, you lose some of the benefits of the reduced contention. If you rebalance only occasionally, it tends to take you longer to rebalance. Ideally, the allocators just wouldn't get out of balance too badly, and if your threads are well balanced, they won't. Dedicated threads doing dedicated jobs tends to make the balance worse. For example, a producer/consumer model where the same threads are always the producer or always the consumer will tend to terribly unbalance. A cool solution in this case, sometimes, is to periodically rotate which threads get which allocators. DS . 0