当我尝试获取(未设置!)当前到期时间时使用 boost expires_from_now() 似乎实际上取消了计时器, 但它实际上按预期运行,但最终没有调用处理程序。
或者换句话说,当使用 expires_from_now() 访问 deadline_timer 时 它会立即调用处理程序,并且在它过期时不会调用处理程序。
请考虑以下代码和相应的输出:
#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;
}
}
20120412 22:41:45689235 before timer construction
20120412 22:41:45689514 before startTimer()
20120412 22:41:45689619 IsRunning: 1
20120412 22:41:45689693 Handler1: Timer 1 was cancelled or retriggered.
20120412 22:41:46689792 IsRunning: 1
20120412 22:41:47689994 IsRunning: 1
20120412 22:41:48690179 IsRunning: 1
20120412 22:41:49690375 IsRunning: 1
20120412 22:41:50690530 IsRunning: 1
20120412 22:41:51690712 IsRunning: 1
20120412 22:41:52690884 IsRunning: 1
20120412 22:41:53691069 IsRunning: 1
20120412 22:41:54691236 IsRunning: 0
20120412 22:41:55691428 IsRunning: 0
20120412 22:41:56691614 IsRunning: 0
20120412 22:41:57691810 IsRunning: 0
20120412 22:41:58692001 IsRunning: 0
20120412 22:41:59692193 IsRunning: 0
20120412 22:42:00692364 IsRunning: 0
20120412 22:42:01692542 IsRunning: 0
20120412 22:42:02692706 IsRunning: 0
20120412 22:42:03692886 IsRunning: 0
20120412 22:42:04693071 IsRunning: 0
20120412 22:42:05693267 IsRunning: 0
20120412 22:42:06693465 IsRunning: 0
最佳答案
When I try to get (not set!) the current expiry time using boost expires_from_now() is seems to actually cancel the timer, yet it actually runs as expected, but does finally not call the handler.
这个假设是不正确的。当您创建 deadline_timer
m_pTimer = new boost::asio::deadline_timer(io_service1);
m_pTimer->async_wait(handler1);
您已经告诉它立即过期。然后,当您启动 io_service
m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
你随后调用
m_pTimer->expires_from_now(boost::posix_time::seconds(10));
作为文档 describes clearly , 这将取消任何未完成的处理程序,强调是我的
This function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the
boost::asio::error::operation_abortederror code.
要解决此问题,请按如下方式更改您的 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()
这会导致预期的行为
mbp:stackoverflow samm$ ./a.out
20120413 11:17:22922529 before timer construction
20120413 11:17:22923155 before startTimer()
20120413 11:17:22923530 IsRunning: 1
20120413 11:17:23924702 IsRunning: 1
20120413 11:17:24925971 IsRunning: 1
20120413 11:17:25927320 IsRunning: 1
20120413 11:17:26928715 IsRunning: 1
20120413 11:17:27929969 IsRunning: 1
20120413 11:17:28930601 IsRunning: 1
20120413 11:17:29931843 IsRunning: 1
20120413 11:17:30933098 IsRunning: 1
20120413 11:17:31934366 IsRunning: 0
20120413 11:17:32923594 Handler1: expired.
20120413 11:17:32934692 IsRunning: 0
20120413 11:17:33935922 IsRunning: 0
20120413 11:17:34936638 IsRunning: 0
20120413 11:17:35937905 IsRunning: 0
20120413 11:17:36939133 IsRunning: 0
20120413 11:17:37940407 IsRunning: 0
20120413 11:17:38941043 IsRunning: 0
20120413 11:17:39942319 IsRunning: 0
20120413 11:17:40942662 IsRunning: 0
20120413 11:17:41943989 IsRunning: 0
20120413 11:17:42945284 IsRunning: 0
20120413 11:17:43946555 IsRunning: 0
mbp:stackoverflow samm$
关于c++ - 为什么 boost asio 函数 expires_from_now() 取消 deadline_timer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10131531/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串