KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I want to create some Timer class, which prints "text" every N seconds, where N will be initialized in constructor. #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp> #include <iostream> class Timer : public boost::enable_shared_from_this<Timer> { public: Timer ( const double interval ) : interval_sec( interval ) { io_service_ = new boost::asio::io_service; timer_ = new boost::asio::deadline_timer ( * io_service_ ); start( ); io_service_ -> run( ); } void start ( ) { timer_ -> expires_from_now( boost::posix_time::seconds( 0 ) ); timer_ -> async_wait(boost::bind( &Timer::handler , shared_from_this( ) , boost::asio::placeholders::error ) ); } private: void handler( const boost::system::error_code& error ) { if ( error ) { std::cerr << error.message( ) << std::endl; return; } printf( "text\n" ); timer_ -> expires_from_now( boost::posix_time::seconds( interval_sec ) ); timer_ -> async_wait( boost::bind( &Timer::handler , shared_from_this( ) , boost::asio::placeholders::error ) ); } private: boost::asio::io_service * io_service_; boost::asio::deadline_timer * timer_; double interval_sec; }; int main() { boost::shared_ptr<Timer> timer( new Timer ( 10 ) ); return 0; } But I have bad_weak_ptr error. terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exceptio
Tags (comma-separated)
Save Edits
Cancel