Fixed a bug in the thread implementation. A lock cannot be used within two different threads.

This commit is contained in:
lotodore
2007-02-18 17:48:36 +00:00
parent 389252aac2
commit fe52316be5
2 changed files with 9 additions and 5 deletions
+6 -4
View File
@@ -47,7 +47,7 @@ private:
}; };
Thread::Thread() Thread::Thread()
: m_isTerminatedMutexLock(m_isTerminatedMutex), m_userReqTerminateLock(m_shouldTerminateMutex) : m_userReqTerminateLock(m_shouldTerminateMutex), m_threadStartBarrier(2)
{ {
} }
@@ -62,7 +62,10 @@ Thread::Run()
boost::mutex::scoped_lock threadLock(m_threadObjMutex); boost::mutex::scoped_lock threadLock(m_threadObjMutex);
if (!m_threadObj.get()) if (!m_threadObj.get())
{
m_threadObj.reset(new boost::thread(ThreadStarter(*this))); m_threadObj.reset(new boost::thread(ThreadStarter(*this)));
m_threadStartBarrier.wait();
}
} }
void void
@@ -114,10 +117,9 @@ Thread::Msleep(unsigned msecs)
void void
Thread::MainWrapper() Thread::MainWrapper()
{ {
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
m_threadStartBarrier.wait();
this->Main(); this->Main();
// Thread has been terminated.
// Unlock the isTerminated mutex.
m_isTerminatedMutexLock.unlock();
} }
bool bool
+3 -1
View File
@@ -22,6 +22,7 @@
#define _THREAD_H_ #define _THREAD_H_
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#ifndef NANOSECONDS_PER_SECOND #ifndef NANOSECONDS_PER_SECOND
@@ -68,7 +69,6 @@ private:
// Flag specifying whether the application code within the // Flag specifying whether the application code within the
// thread was terminated. // thread was terminated.
mutable boost::timed_mutex m_isTerminatedMutex; mutable boost::timed_mutex m_isTerminatedMutex;
mutable boost::timed_mutex::scoped_try_lock m_isTerminatedMutexLock;
// Flag specifying whether the thread should be terminated. // Flag specifying whether the thread should be terminated.
mutable boost::timed_mutex m_shouldTerminateMutex; mutable boost::timed_mutex m_shouldTerminateMutex;
@@ -78,6 +78,8 @@ private:
boost::shared_ptr<boost::thread> m_threadObj; boost::shared_ptr<boost::thread> m_threadObj;
mutable boost::mutex m_threadObjMutex; mutable boost::mutex m_threadObjMutex;
boost::barrier m_threadStartBarrier;
friend class ThreadStarter; friend class ThreadStarter;
}; };