C++ 如何暂停&;恢复推进截止时间计时器?

C++ 如何暂停&;恢复推进截止时间计时器?,c++,boost,boost-asio,C++,Boost,Boost Asio,是否可以使截止时间计时器停止并重新启动 我在C++库中开发了一个播放器程序,需要一个能够暂停和恢复的定时器, 我发现boost deadline timer是一个选项,但停止后如何重新启动它?由于deadline timer使用,您可能可以通过简单的服务暂停计时器。由于deadline timer使用,您可能可以通过简单的服务暂停计时器。由于deadline timer使用,您可以通过简单的服务暂停计时器。由于截止时间计时器使用,您可以通过简单的服务暂停计时器。您的计时器应该异步等待。在这种情况

是否可以使截止时间计时器停止并重新启动

我在C++库中开发了一个播放器程序,需要一个能够暂停和恢复的定时器,


我发现boost deadline timer是一个选项,但停止后如何重新启动它?

由于deadline timer使用,您可能可以通过简单的服务暂停计时器。

由于deadline timer使用,您可能可以通过简单的服务暂停计时器。

由于deadline timer使用,您可以通过简单的服务暂停计时器。

由于截止时间计时器使用,您可以通过简单的服务暂停计时器。

您的计时器应该异步等待。在这种情况下,您可以使用取消计时器,使用deadline_timer::expires_from_now()更改计时器到期时间,并使用再次启动等待

C++03的代码示例如下:

#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>

/// Simple asio::deadline_timer wrapper to aggregate
/// timer, interval and handler function.
class timer
{
    typedef boost::asio::deadline_timer impl;
public:
    typedef impl::duration_type duration;
    typedef boost::function<void (boost::system::error_code, timer&)> handler_function;

    /// Create timer with specified interval and handler
    timer(boost::asio::io_service& io_service, duration interval, handler_function handler)
        : impl_(io_service)
        , interval_(interval)
        , handler_(handler)
    {
    }

    /// Start asynchronous waiting
    void start()
    {
        impl_.expires_from_now(interval_);
        impl_.async_wait(boost::bind(handler_, boost::asio::placeholders::error, boost::ref(*this)));
    }

    /// Stop waiting
    void stop()
    {
        impl_.cancel();
    }

private:
    impl impl_;
    duration interval_;
    handler_function handler_;
};

// Timer handler, just start next tick
void timer_tick(boost::system::error_code err, timer& t)
{
    static int tick_ = 0;
    if (!err)
    {
        std::cout << "tick " << ++tick_ << '\n';
        t.start();
    }
}

boost::asio::io_service io_service;

void timer_thread(timer& t)
{
    t.start();
    io_service.run();
}

int main()
{
    // run a timer in another thread
    timer t(io_service, boost::posix_time::seconds(1), timer_tick);
    boost::thread tthread(&timer_thread, boost::ref(t));

    // stop the timer after some delay
    boost::this_thread::sleep_for(boost::chrono::seconds(3));
    t.stop();

    tthread.join();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
///要聚合的简单asio::deadline\u计时器包装器
///计时器、间隔和处理程序函数。
班级计时器
{
typedef boost::asio::deadline_timer impl;
公众:
typedef impl::duration_type duration;
typedef boost::函数处理程序\函数;
///创建具有指定间隔和处理程序的计时器
计时器(boost::asio::io_服务和io_服务、持续时间间隔、处理程序函数处理程序)
:impl_(io_服务)
,间隔(间隔)
,handler_(handler)
{
}
///启动异步等待
void start()
{
从现在开始执行过期(间隔);
impl_u.async_wait(boost::bind(handler,boost::asio::placeholders::error,boost::ref(*this));
}
///别等了
无效停止()
{
impl_.cancel();
}
私人:
简单简单;
持续时间间隔;
处理函数处理函数;
};
//计时器处理程序,只需开始下一个滴答声
无效计时器(boost::system::error\u code err,timer&t)
{
静态int tick_u2;=0;
如果(!err)
{

std::cout您的计时器应该异步等待。在这种情况下,您可以使用取消计时器,使用deadline更改计时器到期时间\u timer::expires\u from_now()并使用重新启动计时器等待

C++03的代码示例如下:

#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>

/// Simple asio::deadline_timer wrapper to aggregate
/// timer, interval and handler function.
class timer
{
    typedef boost::asio::deadline_timer impl;
public:
    typedef impl::duration_type duration;
    typedef boost::function<void (boost::system::error_code, timer&)> handler_function;

    /// Create timer with specified interval and handler
    timer(boost::asio::io_service& io_service, duration interval, handler_function handler)
        : impl_(io_service)
        , interval_(interval)
        , handler_(handler)
    {
    }

    /// Start asynchronous waiting
    void start()
    {
        impl_.expires_from_now(interval_);
        impl_.async_wait(boost::bind(handler_, boost::asio::placeholders::error, boost::ref(*this)));
    }

    /// Stop waiting
    void stop()
    {
        impl_.cancel();
    }

private:
    impl impl_;
    duration interval_;
    handler_function handler_;
};

// Timer handler, just start next tick
void timer_tick(boost::system::error_code err, timer& t)
{
    static int tick_ = 0;
    if (!err)
    {
        std::cout << "tick " << ++tick_ << '\n';
        t.start();
    }
}

boost::asio::io_service io_service;

void timer_thread(timer& t)
{
    t.start();
    io_service.run();
}

int main()
{
    // run a timer in another thread
    timer t(io_service, boost::posix_time::seconds(1), timer_tick);
    boost::thread tthread(&timer_thread, boost::ref(t));

    // stop the timer after some delay
    boost::this_thread::sleep_for(boost::chrono::seconds(3));
    t.stop();

    tthread.join();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
///要聚合的简单asio::deadline\u计时器包装器
///计时器、间隔和处理程序函数。
班级计时器
{
typedef boost::asio::deadline_timer impl;
公众:
typedef impl::duration_type duration;
typedef boost::函数处理程序\函数;
///创建具有指定间隔和处理程序的计时器
计时器(boost::asio::io_服务和io_服务、持续时间间隔、处理程序函数处理程序)
:impl_(io_服务)
,间隔(间隔)
,handler_(handler)
{
}
///启动异步等待
void start()
{
从现在开始执行过期(间隔);
impl_u.async_wait(boost::bind(handler,boost::asio::placeholders::error,boost::ref(*this));
}
///别等了
无效停止()
{
impl_.cancel();
}
私人:
简单简单;
持续时间间隔;
处理函数处理函数;
};
//计时器处理程序,只需开始下一个滴答声
无效计时器(boost::system::error\u code err,timer&t)
{
静态int tick_u2;=0;
如果(!err)
{

std::cout您的计时器应该异步等待。在这种情况下,您可以使用取消计时器,使用deadline更改计时器到期时间\u timer::expires\u from_now()并使用重新启动计时器等待

C++03的代码示例如下:

#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>

/// Simple asio::deadline_timer wrapper to aggregate
/// timer, interval and handler function.
class timer
{
    typedef boost::asio::deadline_timer impl;
public:
    typedef impl::duration_type duration;
    typedef boost::function<void (boost::system::error_code, timer&)> handler_function;

    /// Create timer with specified interval and handler
    timer(boost::asio::io_service& io_service, duration interval, handler_function handler)
        : impl_(io_service)
        , interval_(interval)
        , handler_(handler)
    {
    }

    /// Start asynchronous waiting
    void start()
    {
        impl_.expires_from_now(interval_);
        impl_.async_wait(boost::bind(handler_, boost::asio::placeholders::error, boost::ref(*this)));
    }

    /// Stop waiting
    void stop()
    {
        impl_.cancel();
    }

private:
    impl impl_;
    duration interval_;
    handler_function handler_;
};

// Timer handler, just start next tick
void timer_tick(boost::system::error_code err, timer& t)
{
    static int tick_ = 0;
    if (!err)
    {
        std::cout << "tick " << ++tick_ << '\n';
        t.start();
    }
}

boost::asio::io_service io_service;

void timer_thread(timer& t)
{
    t.start();
    io_service.run();
}

int main()
{
    // run a timer in another thread
    timer t(io_service, boost::posix_time::seconds(1), timer_tick);
    boost::thread tthread(&timer_thread, boost::ref(t));

    // stop the timer after some delay
    boost::this_thread::sleep_for(boost::chrono::seconds(3));
    t.stop();

    tthread.join();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
///要聚合的简单asio::deadline\u计时器包装器
///计时器、间隔和处理程序函数。
班级计时器
{
typedef boost::asio::deadline_timer impl;
公众:
typedef impl::duration_type duration;
typedef boost::函数处理程序\函数;
///创建具有指定间隔和处理程序的计时器
计时器(boost::asio::io_服务和io_服务、持续时间间隔、处理程序函数处理程序)
:impl_(io_服务)
,间隔(间隔)
,handler_(handler)
{
}
///启动异步等待
void start()
{
从现在开始执行过期(间隔);
impl_u.async_wait(boost::bind(handler,boost::asio::placeholders::error,boost::ref(*this));
}
///别等了
无效停止()
{
impl_.cancel();
}
私人:
简单简单;
持续时间间隔;
处理函数处理函数;
};
//计时器处理程序,只需开始下一个滴答声
无效计时器(boost::system::error\u code err,timer&t)
{
静态int tick_u2;=0;
如果(!err)
{

std::cout您的计时器应该异步等待。在这种情况下,您可以使用取消计时器,使用deadline更改计时器到期时间\u timer::expires\u from_now()并使用重新启动计时器等待

C++03的代码示例如下:

#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/chrono/duration.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>

/// Simple asio::deadline_timer wrapper to aggregate
/// timer, interval and handler function.
class timer
{
    typedef boost::asio::deadline_timer impl;
public:
    typedef impl::duration_type duration;
    typedef boost::function<void (boost::system::error_code, timer&)> handler_function;

    /// Create timer with specified interval and handler
    timer(boost::asio::io_service& io_service, duration interval, handler_function handler)
        : impl_(io_service)
        , interval_(interval)
        , handler_(handler)
    {
    }

    /// Start asynchronous waiting
    void start()
    {
        impl_.expires_from_now(interval_);
        impl_.async_wait(boost::bind(handler_, boost::asio::placeholders::error, boost::ref(*this)));
    }

    /// Stop waiting
    void stop()
    {
        impl_.cancel();
    }

private:
    impl impl_;
    duration interval_;
    handler_function handler_;
};

// Timer handler, just start next tick
void timer_tick(boost::system::error_code err, timer& t)
{
    static int tick_ = 0;
    if (!err)
    {
        std::cout << "tick " << ++tick_ << '\n';
        t.start();
    }
}

boost::asio::io_service io_service;

void timer_thread(timer& t)
{
    t.start();
    io_service.run();
}

int main()
{
    // run a timer in another thread
    timer t(io_service, boost::posix_time::seconds(1), timer_tick);
    boost::thread tthread(&timer_thread, boost::ref(t));

    // stop the timer after some delay
    boost::this_thread::sleep_for(boost::chrono::seconds(3));
    t.stop();

    tthread.join();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
///要聚合的简单asio::deadline\u计时器包装器
///计时器、间隔和处理程序函数。
班级计时器
{
typedef boost::asio::deadline_timer impl;
公众:
typedef impl::duration_type duration;
typedef boost::函数处理程序\函数;
///创建具有指定间隔和处理程序的计时器
计时器(boost::asio::io_服务和io_服务、持续时间间隔、处理程序函数处理程序)
:impl_(io_服务)
,间隔(间隔)
,handler_(handler)
{
}
///启动异步等待
void start()
{
从现在开始执行过期(间隔);
impl_u.async_wait(boost::bind(handler,boost::asio::placeholders::error,boost::ref(*this));
}
///别等了
无效停止()
{
impl_.cancel();
}
私人:
简单简单;
持续时间间隔;
处理函数处理函数;
};
//计时器处理程序,只需开始下一个滴答声
无效计时器(boost::system::error\u code err,timer&t)
{
静态int tick_u2;=0;
如果(!err