Hi, all
I got a runtime
erro when try to stop a boost thread.
I created a
class(listed below) to wrap boost thread, and used it like this:
ThreadWrapper tw(1);//delay 1 seconds
tw.start();
…
tw.stop();
When I called
the function “stop”, a runtime error jump out
said access conflict when running the line(in once.cpp,Line
163):
HANDLE mutex = CreateMutexA(NULL, FALSE, strm.str().c_str());
I’ve tried the class in two MFC projects. One has error, and the
other one ran correctly.
How strange!
Is it a bug of
boost?
Please help me.
//============================================================================
#include <boost/thread.hpp>
#include <boost/bind.hpp>
class ThreadWrapper
{
public:
ThreadWrapper(unsigned delySec=0)
{
m_delySec = delySec;
m_pause = false;
m_stop = false;
}
public:
virtual
~ThreadWrapper(void)
{
}
protected:
boost::mutex m_mutex;
boost::condition m_cond;
bool
m_stop;
bool
m_pause;
unsigned
m_delySec;
public:
virtual
void start()
{
boost::thread mtth(boost::bind(&ThreadWrapper::run, this));
}
virtual
void pause()
{
boost::mutex::scoped_lock lock(m_mutex);
m_pause = true;
m_cond.notify_one();
}
virtual
void resume()
{
boost::mutex::scoped_lock lock(m_mutex);
m_pause = false;
m_cond.notify_one();
}
virtual
void stop()
{
boost::mutex::scoped_lock lock(m_mutex);
m_stop=true;
m_cond.notify_one();
//I tried join, but still got the same error
}
virtual
void run()
{
while(m_stop == false)
{
if (m_pause ==
true)
{
boost::mutex::scoped_lock lock(m_mutex);
while(m_pause
== true)
{
m_cond.wait(lock);
}
}
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += m_delySec;
boost::thread::sleep(xt);
}
}
}
//============================================================================