C++ 为什么boost asio函数从现在起过期()取消截止时间计时器?
当我尝试使用获取(未设置!)当前到期时间时 boost expires_from_now()似乎实际上取消了计时器, 但它实际上按预期运行,但最终没有调用处理程序 换句话说,当访问带有expires\u from\u now()的截止时间计时器时 它将立即调用处理程序,并在处理程序过期时不调用处理程序C++ 为什么boost asio函数从现在起过期()取消截止时间计时器?,c++,linux,boost,boost-asio,C++,Linux,Boost,Boost Asio,当我尝试使用获取(未设置!)当前到期时间时 boost expires_from_now()似乎实际上取消了计时器, 但它实际上按预期运行,但最终没有调用处理程序 换句话说,当访问带有expires\u from\u now()的截止时间计时器时 它将立即调用处理程序,并在处理程序过期时不调用处理程序 请考虑下面的代码和相应的输出: #include <boost/asio.hpp> #include <boost/thread.hpp> #include <b
请考虑下面的代码和相应的输出:
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <iostream>
using namespace boost::posix_time;
using namespace std;
void handler1(const boost::system::error_code &ec)
{
if (ec == boost::asio::error::operation_aborted)
{
std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
}
else
{
std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
}
}
boost::asio::io_service io_service1;
class Mytimer {
public:
Mytimer();
void startTimer();
void runTimerThread();
bool isRunning();
private:
bool m_isRunning;
boost::asio::deadline_timer* m_pTimer;
boost::thread* m_pThread;
};
Mytimer::Mytimer()
: m_pTimer(NULL),
m_pThread(NULL)
{
m_pTimer = new boost::asio::deadline_timer(io_service1);
m_pTimer->async_wait(handler1);
}
void Mytimer::runTimerThread()
{
io_service1.run();
}
void Mytimer::startTimer()
{
m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
m_pTimer->expires_from_now(boost::posix_time::seconds(10));
}
bool Mytimer::isRunning()
{
time_duration td = m_pTimer->expires_from_now();
if (td.total_seconds() > 0)
{
return true;
}
return false;
}
int main()
{
time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f");
std::cout.imbue(std::locale(std::cout.getloc(), facet));
cout << microsec_clock::local_time() << " before timer construction" << endl;
Mytimer timer;
cout << microsec_clock::local_time() << " before startTimer()" << endl;
timer.startTimer();
cout << microsec_clock::local_time() << " IsRunning: " << timer.isRunning() << endl;
for (int i = 0; i <= 20; i++)
{
sleep(1);
cout << microsec_clock::local_time() << " IsRunning: " << timer.isRunning() << endl;
}
}
#包括
#包括
#包括
#包括
#包括
使用名称空间boost::posix_time;
使用名称空间std;
void handler1(const boost::system::error\u code&ec)
{
如果(ec==boost::asio::error::operation_中止)
{
标准::cout
当我尝试使用boost获取(未设置!)当前到期时间时
expires_from_now()似乎实际上取消了计时器,但是
实际按预期运行,但最终不调用处理程序
此假设不正确。当您创建截止时间\u计时器时
m_pTimer = new boost::asio::deadline_timer(io_service1);
m_pTimer->async_wait(handler1);
您告诉它立即过期。然后,当您启动io\u服务时
m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
您随后调用
m_pTimer->expires_from_now(boost::posix_time::seconds(10));
作为文档,这将取消任何未完成的处理程序,重点是我的
此函数设置到期时间。任何挂起的异步等待
操作将被取消。每个取消操作的处理程序
将使用boost::asio::error::operation\u中止的error调用
代码
要解决此问题,请按如下所示更改Mytimer::startTimer()
方法
--- timer4.cc.orig 2012-04-13 11:18:47.000000000 -0500
+++ timer4.cc 2012-04-13 11:16:54.000000000 -0500
@@ -1,4 +1,3 @@
-
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -39,7 +38,6 @@
m_pThread(NULL)
{
m_pTimer = new boost::asio::deadline_timer(io_service1);
- m_pTimer->async_wait(handler1);
}
void Mytimer::runTimerThread()
@@ -49,8 +47,9 @@
void Mytimer::startTimer()
{
- m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
m_pTimer->expires_from_now(boost::posix_time::seconds(10));
+ m_pTimer->async_wait(handler1);
+ m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
}
bool Mytimer::isRunning()
下面的代码现在具有正确的截止时间计时器初始化,
并且有一个私人的布尔标志,可以决定,
如果计时器当前正在运行。它还有一个成员本地处理程序
class Mytimer {
public:
Mytimer();
void startTimer(int timeDelaySecs);
void cancelTimer();
void runTimerThread();
bool isRunning();
void timerHandler(const boost::system::error_code& ec);
private:
bool m_isRunning;
boost::asio::deadline_timer* m_pTimer;
boost::thread* m_pThread;
};
Mytimer::Mytimer()
: m_pTimer(NULL),
m_pThread(NULL)
{
m_pTimer = new boost::asio::deadline_timer(io_service1);
}
void Mytimer::runTimerThread()
{
io_service1.run();
}
void Mytimer::startTimer(int timeDelaySecs)
{
m_pTimer->expires_from_now(boost::posix_time::seconds(timeDelaySecs));
m_pTimer->async_wait(boost::bind(&Mytimer::timerHandler, this, _1));
m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
m_isRunning = true;
}
bool Mytimer::isRunning()
{
return m_isRunning;
}
void Mytimer::timerHandler(const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
}
else
{
std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
}
m_isRunning = false;
}
类Mytimer{
公众:
Mytimer();
void startTimer(int timeDelaySecs);
void cancelTimer();
void runTimerThread();
布尔正在运行();
void timerHandler(const boost::system::error\u code&ec);
私人:
布尔穆正在运行;
boost::asio::deadline\u timer*m\u optimer;
boost::thread*m_pThread;
};
Mytimer::Mytimer()
:m_pTimer(空),
m_pThread(空)
{
m_pTimer=新的boost::asio::deadline_计时器(io_service1);
}
void Mytimer::runTimerThread()
{
io_service1.run();
}
void Mytimer::startTimer(int timeDelaySecs)
{
m_pTimer->expires_from_now(boost::posix_time::seconds(timeDelaySecs));
m_pTimer->async_wait(boost::bind(&Mytimer::timerHandler,this,_1));
m_pThread=newboost::thread(&Mytimer::runTimerThread,this);
m_isRunning=真;
}
bool Mytimer::isRunning()
{
返回m_正在运行;
}
void Mytimer::timerHandler(const boost::system::error\u code&ec)
{
如果(ec==boost::asio::error::operation_中止)
{
std::我认为这里有两个问题:首先在构造函数中调用计时器时没有任何过期时间,因此它实际上会立即过期。第二,当成员函数重新启动计时器时,没有处理程序。因此expires\u from\u现在仍然有效(如您所见,1在10秒后变为0),但将永远不会调用该处理程序,因为没有关联的。我将暂时保留该问题,也许有人可以确认我的结论。您的代码片段非常臃肿,充满了与该问题无关的垃圾,并且/或者使其更难阅读:。感谢您的详细解释。您的输出将打开另一个问题.我如何设计IsRunning()计时器仍在运行时不返回0的成员函数。我想到的一件事是在处理程序中使用一个布尔私有标志,该标志在成员处理程序函数中设置为false。将total_seconds更改为total_nanoses可能是另一个选项。@Michael我不清楚您希望实现什么使用这种方法。一个截止时间\u计时器将在给定的时间后过期,或者被取消。在这种情况下,解决方案是检查给完成处理程序的错误。从上面的输出可以看出,IsRunning成员函数在截止时间\u计时器实际过期之前返回false。因此对我来说,此检查仍然不正确我认为一种可能的方法是设置一个地方标志,见下面我的答案。
class Mytimer {
public:
Mytimer();
void startTimer(int timeDelaySecs);
void cancelTimer();
void runTimerThread();
bool isRunning();
void timerHandler(const boost::system::error_code& ec);
private:
bool m_isRunning;
boost::asio::deadline_timer* m_pTimer;
boost::thread* m_pThread;
};
Mytimer::Mytimer()
: m_pTimer(NULL),
m_pThread(NULL)
{
m_pTimer = new boost::asio::deadline_timer(io_service1);
}
void Mytimer::runTimerThread()
{
io_service1.run();
}
void Mytimer::startTimer(int timeDelaySecs)
{
m_pTimer->expires_from_now(boost::posix_time::seconds(timeDelaySecs));
m_pTimer->async_wait(boost::bind(&Mytimer::timerHandler, this, _1));
m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
m_isRunning = true;
}
bool Mytimer::isRunning()
{
return m_isRunning;
}
void Mytimer::timerHandler(const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
}
else
{
std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
}
m_isRunning = false;
}