下面是一个用计时器包装线程的测试类的实现。奇怪的是,如果最后期限设置为 500 毫秒,它可以工作,但如果我将其设置为 1000 毫秒,它不会。我究竟做错了什么?
#include "TestTimer.hpp"
#include "../SysMLmodel/Package1/Package1.hpp"
TestTimer::TestTimer(){
thread = boost::thread(boost::bind(&TestTimer::classifierBehavior,this));
timer = new boost::asio::deadline_timer(service,boost::posix_time::milliseconds(1000));
timer->async_wait(boost::bind(&TestTimer::timerBehavior, this));
};
TestTimer::~TestTimer(){
}
void TestTimer::classifierBehavior(){
service.run();
};
void TestTimer::timerBehavior(){
std::cout<<"timerBehavior\r";
timer->expires_at(timer->expires_at() + boost::posix_time::milliseconds(1000));
timer->async_wait(boost::bind(&TestTimer::timerBehavior,this));
}
更新 1 我注意到程序卡住了(或者至少在控制台中的标准输出停留了很多秒,大约 30 秒)然后很多“timerBehavior”字符串被一起打印出来,就好像它们已经在某个地方排队一样。