Runtime error when try to stop boost thread

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 project. 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) { 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(); } 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); } } } //========================================================================== ==

hp.shen@hyaptech.com wrote: I can't see exactly why you get an error, but here are some observations:
ThreadWrapper(unsigned delySec=0) { delySec = delySec;
I think you meant m_delySec = delySec.
m_pause = false; m_stop = false; }
What's wrong with: ThreadWrapper(unsigned delaySec=0): m_stop(false), m_pause(false), m_delaySec(delaySec) {}
virtual void stop() { boost::mutex::scoped_lock lock(m_mutex); m_stop=true; m_cond.notify_one();
Maybe you want to join the thread here?
}
Phil.
participants (2)
-
Phil Endecott
-
申和平