|
Boost Users : |
Subject: Re: [Boost-users] [thread] : pause/resume thread
From: bit bit (g_rambot_at_[hidden])
Date: 2011-04-15 09:36:29
inside qthread contructor, copy just the following and you are good to go.
mThread = new boost::thread(bind(&qthread::Execute, this));
Date: Fri, 15 Apr 2011 11:06:56 +0100
From: sfx810_at_[hidden]
To: boost-users_at_[hidden]
Subject: [Boost-users] [thread] : pause/resume thread
Hi All, I have just started learning boost. I want my thread to support following functions
Pause();
Resume();
Stop();.
I have come up with following code, which ends up as dead lock. Can some one please guide me as how to achieve the desired functionality.
Would really appreciate any help.
Thanks.
Kaz
class qthread
{
public:
boost::thread* mThread;
boost::condition_variable mCond;
boost::mutex mMutex;
bool mExit;
qthread(): mExit(false)
{
// create function pointer to local Execute function
boost::function<void (qthread*)> fptr = &qthread::Execute;
fptr(this);
// pass on function pointer along with this reference to boost thread
boost::thread th0( fptr, this);
// assign temp thread to local variable
mThread = &th0;
}
void Resume()
{
mMutex.unlock();
mCond.notify_all();
}
void Stop()
{
mThread->interrupt();
mExit = true;
Resume();
}
void Pause()
{
// todo
}
virtual void Execute()
{
std::cout<<"Entering thread Execution"<<std::endl;
boost::unique_lock<boost::mutex> lock(mMutex);
mCond.notify_all();
mCond.wait(lock); //<< DEAD LOCK
int i = 0;
while ( !mExit )
{
try
{
while ( 1 )
{
std::cout<<"Executing thread, cycle i = "<<++i<<std::endl;
boost::this_thread::interruption_point();
}
}
catch( boost::thread_interrupted&)
{
std::cout<<"thread_interrupt exception"<<std::endl;
mCond.wait(lock);
}
}
std::cout<<"Exiting thread"<<std::endl;
}
};
//--------------------------
int main()
{
qthread qt; ///<< DEAD LOCK
boost::this_thread::sleep(boost::posix_time::seconds(3) );
qt.Resume();
boost::this_thread::sleep(boost::posix_time::seconds(3) );
qt.Stop();
}
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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