title: Boost thread overrides value of member variables
body: <p>I am currently working on a project where we try to develop a video game for blind and sighted people using the Microsoft Kinect, haptic feedback devices like the PAHANTOM and stereo rendering for different 3D devices. In the current situation everything runs within a single thread and as you could imagine the application lacks performance since the haptic devices need to run their control loop with at least 1000 Hz. What I am trying to do here is to create 4 threads, one for the rendering done with Ogre3D, one for the Collision Detection, one for the haptic and the last will manage the data collected by the Kinect. </p>
  
  <p>Each part has its own manager class such as KinectManager, HapticManager etc. Because we are using Ogre3D we have class called <code>MainApplication</code> which inherits from <code>BaseApplication</code> in which all necessary functions such as <code>setup()</code>, <code>configure()</code> etc. are implemented. Within the <code>MainApplication</code> the function <code>createScene()</code> is called in which the <code>ColisionManager</code> is created:</p>
  
  <pre><code>void MainApplication::createScene()
  {
      // Initialize CollisionManager
      CollisionManager cManager;
      cManager.run();
  
      // Set the scene's ambient light
      mSceneMgr-&gt;setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
  
      // Create a Light and set its position
      Ogre::Light* light = mSceneMgr-&gt;createLight("MainLight");
      light-&gt;setPosition(20.0f, 80.0f, 50.0f);
  
      // Haptic indicator
      Ogre::Entity* hapticJoystick = mSceneMgr-&gt;createEntity("hapticPos", "Icosphere.mesh");
      hapticNode = mSceneMgr-&gt;getRootSceneNode()-&gt;createChildSceneNode("hapticPos");
      hapticNode-&gt;attachObject(hapticJoystick);
  }
  </code></pre>
  
  <p>The <code>CollisionManager.h</code> looks like this:</p>
  
  <pre><code>#ifndef __COLLISIONMANAGER_H_
  #define __COLLISIONMANAGER_H_
  
  #include "HapticManager.h"
  #include "KinectManager.h"
  
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  #include &lt;boost/thread/mutex.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  
  #include &lt;OgreManualObject.h&gt;
  #include &lt;OgreSceneManager.h&gt;
  
  bool cManagerReady = false;
  
  class CollisionManager
  {
  public:
      CollisionManager();
      virtual ~CollisionManager();
  
      void run();
      bool isBusy();
  
  private:
      KinectManager *kManager;
      HapticManager *hManager;
  
      bool mBusy;
      bool mShutdown;
      boost::shared_ptr&lt;boost::thread&gt; mThread;
      boost::mutex mMutex;
  
      void work();
  };
  
  #endif // __COLLISIONMANAGER_H_
  </code></pre>
  
  <p>The <code>run()</code> function from the <code>CollisionManager</code> is called by the <code>MainApplication</code> to initialize and start the thread for the collision detection. The <code>work()</code> function will do all the work and needs to call non static member variables and functions.</p>
  
  <p>The implementation looks like this:</p>
  
  <pre><code>#include "CollisionManager.h"
  
  using namespace std;
  
  CollisionManager::CollisionManager() :
      kManager ( new KinectManager ),
      hManager ( new HapticManager ),
      mShutdown ( false ),
      mBusy ( false )
  {
  }
  
  CollisionManager::~CollisionManager(void)
  {
  }
  
  bool CollisionManager::isBusy()
  {
      return this-&gt;mBusy;
  }
  
  void CollisionManager::run()
  {
      if (!this-&gt;mBusy) 
      {
          this-&gt;mBusy = true;
          this-&gt;mThread = boost::shared_ptr&lt;boost::thread&gt;(new boost::thread(boost::bind(&amp;CollisionManager::work, this)));
      }
  }
  
  void CollisionManager::work()
  {
      while(!mShutdown)
      {
          cout &lt;&lt; boolalpha &lt;&lt; mShutdown &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Currently I create the thread with a <code>boost::shared_ptr</code> because I was running into the problem that I could not access non static member variables. Now at least I solved this problem by using the <code>shared_ptr</code>. Within the <code>work()</code> function the thread should run as long as the <code>mSutdown</code> variable is set to false which is the initial value assigned in the <code>constructor</code>. Somehow the value of <code>mShutdown</code> is changed by something and cannot figure out where and what is changing the value since I am not explicitly changing it. The behaviour is that the thread stops running because the flag turns <code>true</code>. Does this phenomenon tend to occur when using the <code>shared_ptr</code> from <code>boost</code>?</p>