Boost logo

Boost Users :

Subject: Re: [Boost-users] Race condition with Boost::Thread
From: Craig Henderson (cdm.henderson_at_[hidden])
Date: 2009-07-29 10:46:49


2009/7/29 Craig Henderson <cdm.henderson_at_[hidden]>

>
> class WorkerThread
> {
> public:
> typedef boost::function<void ()> JobType;
>
> WorkerThread()
> {
> m_Thread = boost::thread(boost::bind(&WorkerThread::run,
> boost::ref(this)));
> }
>
> ~WorkerThread()
> {
> while (!m_Running)
> ;
> m_Thread.join();
> m_Running = false;
> }
>
> const bool isRunning() const
> {
> return m_Running;
> }
>
> private:
> volatile bool m_Running;
> boost::thread m_Thread;
>
> void run()
> {
> m_Running = true;
> BOOST_ASSERT(isRunning());
> }
>
> private:
> WorkerThread(const WorkerThread& rhs);
> WorkerThread& operator = (const WorkerThread& rhs);
> };
>
> Regards
> -- Craig
>

You need to initial m_Running=false; in the ctor, too.
-- Craig



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net