English 中文(简体)
你们如何从夸大的提款期限中歧视被取消者? 时间
原标题:How do you discriminate a cancelled from a retriggered boost deadline_timer

When you call expires_from_now() on a running timer, the timer is cancelled, and a new timer is called. So the associated handler is called. It is easy to discriminate in the handler between a cancelled and an expired timer. Yet, I am wondering, if there is a way to discriminate between a expired and a retriggered timer. In both instances the handler is called with error_code operation_aborted. Or maybe I missing some details.

以下守则产生了以下产出:

20120415 21:32:28079507 Main: Timer1 set to 15 s.    
20120415 21:32:28079798 Main: Timer1 set to 12 s.    
20120415 21:32:28079916 Handler1: Timer 1 was cancelled or retriggered.    
20120415 21:32:40079860 Handler1: expired.

This suggests that the handler is not able to implement actions for a cancelled handler, because retriggering a timer will call the same handler and thereby execute the same actions. Which is probably not the intended behaviour.

#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)
    {
        cout << microsec_clock::local_time() << " Handler1: Timer was cancelled or retriggered." << endl; 
    }
    else
    {
        cout << microsec_clock::local_time() << " Handler1: Timer expired." << endl; 
    }
} 

boost::asio::io_service io_service1; 

void run1() 
{ 
    io_service1.run(); 
} 

int main() 
{ 
    time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f");
    cout.imbue(locale(cout.getloc(), facet));

    boost::asio::deadline_timer timer1(io_service1, seconds(15)); 
    timer1.async_wait(handler1); 
    cout << microsec_clock::local_time() << " Main: Timer1 set to 15 s." << endl; 
    // now actually run the timer
    boost::thread thread1(run1); 
    timer1.expires_from_now(seconds(12));
    cout << microsec_clock::local_time() << " Main: Timer1 set to 12 s." << endl; 
    // here the timer is running, but we need to reset the deadline
    timer1.async_wait(handler1); 
    thread1.join();   // wait for thread1 to terminate 
} 
最佳回答

d 建议在使用构成的情况下,设立一个组,以总结<代码>deadline_timer。 取消该国旗时,该国旗被取消。 在手里,重新打上国旗。 当援引<代码>expires_ From_now()时,没有打上国旗。

#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> 

class Timer
{
public:
    Timer(
            const std::string& name,
            boost::asio::io_service& io_service
         ) :
        _name( name ),
        _timer( io_service ),
        _cancelled( false )
    {
        _timer.expires_from_now( boost::posix_time::seconds(0) );
        this->wait();
    }

    void wait()
    {
        _timer.async_wait(
                boost::bind(
                    &Timer::handler,
                    this,
                    boost::asio::placeholders::error
                    )
                );
    }

    void cancel() { 
        _cancelled = true;
        _timer.cancel(); 
    }

    void restart() {
        _timer.expires_from_now( boost::posix_time::seconds(5) );
    }

private:
    void handler(
            const boost::system::error_code& error
            )
    {
        if ( !error ) {
            std::cout << _name << " " << __FUNCTION__ << std::endl;
            _timer.expires_from_now( boost::posix_time::seconds(5) );
            this->wait();
        } else if ( error == boost::asio::error::operation_aborted && _cancelled ) {
            _cancelled = false;
            std::cout << _name << " " << __FUNCTION__ << " cancelled" << std::endl;
        } else if ( error == boost::asio::error::operation_aborted ) {
            std::cout  << _name << " " << __FUNCTION__ << " retriggered" << std::endl;
            this->wait();
        } else {
            std::cout << "other error: " << boost::system::system_error(error).what() << std::endl;
        }
    }

private:
    const std::string _name;
    boost::asio::deadline_timer _timer;
    bool _cancelled;
};

int
main()
{
    boost::asio::io_service ios;
    Timer timer1( "timer1", ios );
    Timer timer2( "timer2", ios );

    boost::thread thread(
            boost::bind(
                &boost::asio::io_service::run,
                boost::ref(ios)
                )
            );

    sleep( 3 );
    std::cout << "cancelling" << std::endl;
    timer1.cancel();
    timer2.restart();

    thread.join();
}

样本会议

macmini:stackoverflow samm$ ./a.out
timer1 handler
timer2 handler
cancelling
timer1 handler cancelled
timer2 handler retriggered
timer2 handler
^C
macmini:stackoverflow samm$ 
问题回答

I don t know any way and probably there is no good way to do it (there is nothing in the documentation saying that you can tell apart these two situations).

我认为,这是按目的进行的。 在新的到期时间上设置<条码>deline_timer,可免除以前的任何手稿,因为:

  • sometimes it is hard to tell if something was already waiting on the timer, therefore it d be hard to tell what exactly will happen, when you set an expiry time;
  • this is an easy way to prevent sheduled events from running twice, requiring to handle another error code in a special way is more error-prone.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签