boost :: asio :: deadline_timer和std :: chrono时间值 [英] boost::asio::deadline_timer with std::chrono time values
问题描述
我有一个使用asio截止时间计时器的应用程序。应用程序的其余部分使用 std :: chrono
构造其时间值,使用 boost :: posix_time $ c $感觉很尴尬c>只有触摸asio的东西。如果可以,为了一致性,可读性等,我想在整个应用程序中使用
std :: chrono
。
I have an application that uses asio deadline timers. The rest of the application uses std::chrono
constructs for its time values, and it feels awkward to use boost::posix_time
for only the stuff that touches asio. I'd like to use std::chrono
throughout the application if I can, for consistency, readability, etc.
在我看来,答案将涉及使用定时器的模板:
It seems to me that the answer would involve using the timer's template:
typedef boost::asio::basic_deadline_timer<std::chrono::system_clock::time_point>
my_deadline_timer_type;
my_deadline_timer_type a_timer(io_service);
除了这在编译时出现严重错误...很多行错误,到此:
Except this blows up badly at compile time...many lines of error, most of which are similar to this:
/opt/arm/include/boost/asio/deadline_timer_service.hpp:51:43:错误:无效使用不完整类型'boost :: asio :: deadline_timer_service>>,boost :: asio :: time_traits>>>> :: traits_type {aka struct boost :: asio :: time_traits>>>}'
/opt/arm/include/boost/asio/deadline_timer_service.hpp:51:43: error: invalid use of incomplete type 'boost::asio::deadline_timer_service > >, boost::asio::time_traits > > > >::traits_type {aka struct boost::asio::time_traits > > >}'
所以,看起来我可能需要创建一个新的 traits_type
并声明一个 deadline_timer_service
使用它,但我不知道如何/在哪里。我不得不相信这个问题已经解决了。我使用g ++ 4.7.3在linux上使用-std = c ++ 11,交叉编译到arm。
So, it looks like I may need to create a new traits_type
and declare a deadline_timer_service
using it, but I'm not sure how/where. I have to believe this problem has been solved. I'm using g++ 4.7.3 with -std=c++11 on linux, cross-compiling to arm.
推荐答案
如果您使用boost 1.49或更高版本,ASIO推出了 basic_waitable_timer ,它使用来自 std :: chrono
或 boost :: chrono
的C ++ 11兼容时钟。它们还为 steady_clock
, system_clock
和 high_resolution_clock的boost版本提供了预定义的typedef
。它的功能与 deadline_timer
相同,但使用C ++ 11时钟机制,而不是 boost :: posix_time
结构。
If you are using boost 1.49 or later, ASIO introduced the basic_waitable_timer, which uses C++11 compatible clocks, either from std::chrono
or boost::chrono
. They also provided pre-defined typedefs for the boost versions of steady_clock
, system_clock
, and high_resolution_clock
. Its functionality is the same as deadline_timer
, but uses the C++11 clock mechanisms instead of the boost::posix_time
structures.
对于早期版本,你必须传递一个traits结构来处理 deadline_timer
。请参阅ASIO TimeTraits 要求。大多数是微不足道的,最后不是。因此,例如
For earlier versions, you're going to have to pass a traits structure to handle conversion to the type expected by deadline_timer
. See the ASIO TimeTraits requirements. Most are trivial, the last is not. So, for example
template<typename Clock>
struct CXX11Traits
{
typedef typename Clock::time_point time_type;
typedef typename Clock::duration duration_type;
static time_type now()
{
return Clock::now();
}
static time_type add(time_type t, duration_type d)
{
return t + d;
}
static duration subtract(time_type t1, time_type t2)
{
return t1-t2;
}
static bool less_than(time_type t1, time_type t2)
{
return t1 < t2;
}
static boost::posix_time::time_duration
to_posix_duration(duration_type d1)
{
using std::chrono::duration_cast;
auto in_sec = duration_cast<std::chrono::seconds>(d1);
auto in_usec = duration_cast<std::chrono::microseconds>(d1 - in_sec);
boost::posix_time::time_duration result =
boost::posix_time::seconds(in_sec.count()) +
boost::posix_time::microseconds(in_usec.count());
return result;
}
};
然后,您将为每个要使用的C ++ 11时钟创建截止时间计时器。这里的例子是 std :: system_clock
。您将使用此截止时间计时器作为正常。
You would then create deadline timers for each C++11 clock you want to use. The example here is for std::system_clock
. You would use this deadline timer as normal.
typedef boost::asio::basic_deadline_timer<
std::system_clock,
CXX11Traits<std::system_clock>> my_system_clock_deadline_timer
这篇关于boost :: asio :: deadline_timer和std :: chrono时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!