我正在读一些书,我得到了一些计时器实现。然而书中并没有对计时器进行真正的解释,我想知道他为什么要做某些事情。
代码:
// Desc: Use this class to regulate code flow (for an update function say)
// Instantiate the class with the frequency you would like your code
// section to flow (like 10 times per second) and then only allow
// the program flow to continue if Ready() returns true
class Regulator
{
private:
//the time period between updates
double m_dUpdatePeriod;
//the next time the regulator allows code flow
DWORD m_dwNextUpdateTime;
public:
Regulator(double NumUpdatesPerSecondRqd)
{
m_dwNextUpdateTime = (DWORD)(timeGetTime()+RandFloat()*1000);
if (NumUpdatesPerSecondRqd > 0)
{
m_dUpdatePeriod = 1000.0 / NumUpdatesPerSecondRqd;
}
else if (isEqual(0.0, NumUpdatesPerSecondRqd))
{
m_dUpdatePeriod = 0.0;
}
else if (NumUpdatesPerSecondRqd < 0)
{
m_dUpdatePeriod = -1;
}
}
//returns true if the current time exceeds m_dwNextUpdateTime
bool isReady()
{
//if a regulator is instantiated with a zero freq then it goes into
//stealth mode (doesn't regulate)
if (isEqual(0.0, m_dUpdatePeriod)) return true;
//if the regulator is instantiated with a negative freq then it will
//never allow the code to flow
if (m_dUpdatePeriod < 0) return false;
DWORD CurrentTime = timeGetTime();
//the number of milliseconds the update period can vary per required
//update-step. This is here to make sure any multiple clients of this class
//have their updates spread evenly
static const double UpdatePeriodVariator = 10.0;
if (CurrentTime >= m_dwNextUpdateTime)
{
m_dwNextUpdateTime = (DWORD)(CurrentTime + m_dUpdatePeriod + RandInRange(-UpdatePeriodVariator, UpdatePeriodVariator));
return true;
}
return false;
}
};
isEqual() 检查两个给定 double 之间的差异是否小于 0.00...1
RandInRange() 返回给定范围内的随机数
我真的不明白为什么他要将给定范围内的随机数添加到 m_dwNextUpdateTime。尽管他‘解释’了,但对我来说似乎没有任何意义。
有人能帮忙吗?
最佳答案
// This is here to make sure any multiple clients of this class
//have their updates spread evenly
引入随机性是为了在您同时多次运行相同代码时降低争用。如果所有客户端使用完全相同的时间间隔,它们可能会在完全相同的时间全部“醒来”,这在负载方面可能是不幸的。引入一些随机变化可以使他们的事件更加分散。
关于c++ - 为什么要根据随机值更新定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8498848/
类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
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个模型: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
它不等于主线程的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类的两个特殊实例的字符串
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象