简体   繁体   English

提升deadline_timer问题

[英]boost deadline_timer issue

Here follows the implementation of a test class wrapping a thread with a timer. 下面是使用计时器包装线程的测试类的实现。 The strange thing is that if the deadline is set to 500 milliseconds it works but if I set it to 1000 milliseconds it does not. 奇怪的是,如果截止日期设置为500毫秒它可以工作,但如果我将它设置为1000毫秒,它不会。 What am I doing wrong? 我究竟做错了什么?

#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));
}

UPDATE 1 I have noticed that the program stucks (or at least the standard output in the console for many seconds, about 30) then a lot of "timerBehavior" strings are printed out together as if they have been queued somewhere. 更新1我注意到程序卡住了(或者至少控制台中的标准输出很多秒,大约30秒)然后很多“timerBehavior”字符串被打印出来,好像它们已经在某处排队一样。

You program might have several problems. 您的程序可能有几个问题。 From what you have shown, it's hard to say, if the program stops before the timer had a chance to trigger. 从你所展示的内容来看,很难说,如果程序在计时器有机会触发之前停止。 And, you do not flush your output, use std::endl, if you want to flush the output after a newline. 并且,如果要在换行符后刷新输出,则不要刷新输出,使用std :: endl。 Third, if your thread is going to run the io_service.run() function, it might be that the thread finds an empty io queue and run() will return immediately. 第三,如果你的线程要运行io_service.run()函数,那么线程可能会找到一个空的io队列,run()会立即返回。 To prevent that, there is a work class, that will prevent this. 为了防止这种情况,有一个工作类可以防止这种情况发生。 Here is my example, from you code, that might work as expected: 以下是我的代码,可能会按预期工作:

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>

class TestTimer
{
public:
    TestTimer()
        : service()
        , work( service )
        , thread( boost::bind( &TestTimer::classifierBehavior,this ) )
        , timer( service,boost::posix_time::milliseconds( 1000 ) )
    {
        timer.async_wait( boost::bind( &TestTimer::timerBehavior, this ) );
    }

    ~TestTimer()
    {
        thread.join();
    }
private:
    void classifierBehavior()
    {
        service.run();
    }


    void timerBehavior() {
        std::cout << "timerBehavior" << std::endl;
        timer.expires_at( timer.expires_at() + boost::posix_time::milliseconds( 1000 ) );
        timer.async_wait( boost::bind( &TestTimer::timerBehavior,this ) );
    }

    boost::asio::io_service         service;
    boost::asio::io_service::work   work;
    boost::thread                   thread;
    boost::asio::deadline_timer     timer;
};

int main()
{
    TestTimer test;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM