Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have some guess of the preferred size of the pool you can create fixed size pool using stack structure using array (the fastest possible solution). Then you need to implement four phases of object life time hard initialization (and memory allocation), soft initialization, soft cleanup and hard cleanup (and memory release). Now in pseudo code:</p> <pre><code>Object* ObjectPool::AcquireObject() { Object* object = 0; lock( _stackLock ); if( _stackIndex ) object = _stack[ --_stackIndex ]; unlock( _stackLock ); if( !object ) object = HardInit(); SoftInit( object ); } void ObjectPool::ReleaseObject(Object* object) { SoftCleanup( object ); lock( _stackLock ); if( _stackIndex &lt; _maxSize ) { object = _stack[ _stackIndex++ ]; unlock( _stackLock ); } else { unlock( _stack ); HardCleanup( object ); } } </code></pre> <p>HardInit/HardCleanup method performs full object initialization and destruction and they are executed only if the pool is empty or if the freed object cannot fit the pool because it is full. SoftIniti performs soft initialization of objects, it initializes only those aspect of objects that can be changed since it was released. SoftCleanup method free resources used by the object which should be freed as fast as possible or those resources which can become invalid during the time its owner resides in the pool. As you can see locking is minimal, only two lines of code (or only few instructions).</p> <p>These four methods can be implemented in separate (template) classes so you can implement fine tuned operations per object type or usage. Also you may consider using smart pointers to automatically return object to its pool when it is no longer needed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload