Reputation: 1125
I have a question about boost asio timers and safe-thread work with them. Let's say, i have the following class:
CTest.h:
class CTest
{
boost::asio::io_service io;
boost::asio::deadline_timer timer;
void Check();
CTest ();
~CTest ();
};
And CTest.cpp:
CTest::CTest():
timer (io, boost::posix_time::seconds(0))
{
timer.async_wait(boost::bind(&CTest::Check, this));
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
}
void CTest::Check()
{
//some work
m_timer.expires_from_now(boost::posix_time::seconds(10));
m_timer.async_wait(boost::bind(&CTest::Check, this));
}
CTest::~CTest()
{
io.stop();
}
So, the question is how to finish Check thread safe? I mean, this Check function can be called after destructor, and we have a crash.
Upvotes: 1
Views: 968
Reputation: 7357
Your destructor should stop all timers, so they will not be called after object is destroyed: m_timer.cancel()
. Also, if you want to prevent simultaneous methods calls on same object, wrap your boost::bind with boost::asio::strand::wrap. Check out asio documentation for examples.
Upvotes: 1
Reputation: 18339
Using io_service::stop() is very rarely what you want to do.
A few points:
this
. In the callback function, convert the weak_ptr to a shared_ptr and if that is valid then call the method.Just a few points. Not going to rewrite your code for you.
Upvotes: 2