使用Boost基本截止时间计时器来调用类方法
Using a Boost basic deadline timer to call a class method
我想绑定一个定时器回调方法在我的类。你能帮我修改一下代码吗?
tftp_connection::tftp_connection (std::string id,
std::string file_name,
connection_type_enum connection_type,
tftp_server* server,
boost::asio::io_service& io_service)
: timer(io_service, boost::posix_time::milliseconds(5000)) {
...
//timer.async_wait(callback);
timer.async_wait(boost::bind(&tftp_connection::callback ), this);
...
}
...
void tftp_connection::callback(const boost::system::error_code& /*error*/)
{
// TIME OUT
}
错误是:
Compilation error:
/usr/local/boost_1_55_0/boost/bind/bind.hpp:69:22: Type 'void (tftp_connection::*)(const boost::system::error_code &)' cannot be used prior to '::' because it has no members
timer.async_wait(boost::bind(&tftp_connection::callback ), this);
应该timer.async_wait(boost::bind(&tftp_connection::callback, this));
需要将对象绑定到成员函数,因为成员函数离开它所属的对象就不能工作。
参见boost示例,使用async_wait
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 如何将迭代器调用转发给类的私有成员?
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++延迟后的优化器调用函数
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 我希望计时器只在上一个调用返回或完成执行时调用代码
- C++:创建一个可以调用任何函数的计时器/警报类
- 为什么从 win32 计时器回调抛出时不调用我的析构函数?
- 创建一个计时器在X秒内调用一个函数
- 使用Boost基本截止时间计时器来调用类方法