Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make my component entity system thread safe?
    primarykey
    data
    text
    <p>I am currently integrating an entity component system, as seen <a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=463508" rel="nofollow noreferrer">here</a>, with a physics engine and a graphics engine. This was all fine until recently deciding that the physics should be running in its own thread. (Thanks Glenn Fiedler!)</p> <p>As it is now I am simply locking a mutex shared by all subsystems when accessing components.</p> <p>Snippet from the physics loop:</p> <pre><code>lock_guard&lt;mutex&gt; lock( m_EntMutex ); entitymap::iterator it; for ( it = m_Ents.begin(); it != m_Ents.end(); ++it ) { // Get physics component from entity // This is guaranteed to work ( component must exist for it to present in the map ) shared_ptr&lt;comp_phys&gt; phys( static_cast&lt;comp_phys*&gt;( it-&gt;second-&gt;getComponent( COMP_PHYS ).lock().get() ) ); // Get resulting Box2D vector b2Vec2 vec = phys-&gt;getBody()-&gt;GetPosition(); // Get position component from entity // Same as above, but this is the component shared with the graphics subsystem shared_ptr&lt;comp_pos&gt; pos( static_cast&lt;comp_pos*&gt;( it-&gt;second-&gt;getComponent( COMP_POS ).lock().get() ) ); // Update position component from Box2D vector pos-&gt;setPosition( vec.x, vec.y, 0 ); } </code></pre> <p>Snippet from graphics loop:</p> <pre><code>lock_guard&lt;mutex&gt; lock( m_EntMutex ); entitymap::iterator it; for ( it = m_Ents.begin(); it != m_Ents.end(); ++it ) { // Get position component from entity // This is shared with the physics subsystem shared_ptr&lt;comp_pos&gt; pos( static_cast&lt;comp_pos*&gt;( it-&gt;second-&gt;getComponent( COMP_POS ).lock().get() ) ); // Get position from position component doubleVec3 vec = p-&gt;getPosition(); // Get graphics component from entity shared_ptr&lt;comp_gfx&gt; gfx( static_cast&lt;comp_gfx*&gt;( it-&gt;second-&gt;getComponent( COMP_GFX ).lock().get() ) ); // Update graphics component from position component gfx-&gt;getObject()-&gt;getParentSceneNode()-&gt;setPosition( float(vec.x), float(vec.y), float(vec.z) ); } </code></pre> <p>This is obviously a very naïve implementation, so I tried making the individual components have their own mutexes. It seemed the logical performance choice, but then the physics results (as queried through the position component) wouldn't always be consistent and reliable.</p> <p>What would be the most efficient manner of accomplishing a smooth updating process? Should I let it update the entire world in one go or do something more incremental?</p> <p><strong>Edit:</strong> It has come to my attention that the pointer acquisition scheme is flawed, but let us assume that the pointers are valid.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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