Quote:Original post by kiske1
I have this class (only relevant code):
class cBall{private: cBall(); static cBall* m_pGlobalBall;
Try really hard to avoid class-/file-/function-static and global objects of any kind in threaded code.
Quote:
And there's the implementation
// ...void cBall::StartUpdateThread(){ m_BallThread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&cBall::UpdatePosition, this))); m_bThreadStop = FALSE;}
These two statements are in the wrong order. It's entirely possible that one iteration of the loop in UpdatePosition will occur before m_bThreadStop is set to FALSE. Perhaps this is unrelated to your current problem, but it is a bug nonetheless.
You can also write the thread creation line somewhat more succinctly:
m_BallThread.reset(new boost::thread(boost::bind(&cBall::UpdatePosition, this)));
Quote:
// ...void cBall::StopUpdateThread(){ m_bThreadStop = TRUE; m_BallThread->join();}
m_bThreadStop should be protected by the mutex as it is read from and written to by different threads. If you don't do this, old values may stay resident in one of the various layers of cache.
Quote:
// ...void cBall::Render(){ if (!m_bDataRead) { boost::mutex::scoped_lock l(m_BallMutex); m_NodeBall->setPosition(m_VPosizioneReale); m_bDataRead = TRUE; }}
You have the same issue again here with m_bDataRead.
You need to have a very clear idea of the invariants that your mutex is protecting. All the levels of the cache must have a consistent view of your shared data for the threads to be properly coordinated.