C++中的超时变量使用什么类型
What type to use for a timeout variable in C++?
当在C++中编写一个将超时作为其参数之一的函数时,我应该为超时参数本身使用什么类型?
此类函数的示例可能是:
void my_function(bool work_really_hard, timeout_type timeout)
{
// Do stuff, until timeout is reached.
}
我曾考虑过将std::chrono::seconds
用于timeout_type
,但这不允许在亚秒级领域使用任何超时。
改用std::chrono::nanoseconds
时,指定 10 分钟很麻烦。
有什么方法可以合理地解决这个问题,同时保持函数签名和调用整洁简单?
当使用 std::chrono::nanoseconds 时,它很麻烦 指定,比如说,10 分钟。
实际上,它丝毫没有使它变得繁琐:
#include <chrono>
#include <iostream>
void my_function(bool work_really_hard, std::chrono::nanoseconds timeout)
{
std::cout << timeout.count() << 'n';
}
int main()
{
my_function(true, std::chrono::minutes(10));
}
输出:
600000000000
您唯一遇到nanoseconds
麻烦的情况是,如果您想传入不会完全转换为nanoseconds
的东西,例如 picoseconds
或duration<long, ratio<1, 3>>
(1/3 秒单位)。
更新
我打算这个答案是一个已经接受的答案的附加信息,我认为这是一个很好的答案(由 sehe)。 sehe 推荐了一个模板化的解决方案,我也认为很好。
如果你想接受任何std::chrono::duration
,即使是你可能不得不截断或四舍五入的,那么使用 sehe 删除的答案是要走的路:
template <typename Rep, typename Period>
void my_function(bool work_really_hard, std::chrono::duration<Rep, Period> timeout)
{
// Do stuff, until timeout is reached.
std::this_thread::sleep_for(timeout);
}
如果由于某种原因您不想处理模板和/或您满足于让您的客户必须仅指定完全可转换为std::chrono:nanoseconds
的单位,那么使用我上面显示的std::chrono:nanoseconds
也是完全可以接受的。
所有std::chrono
"预定义"单元:
hours
minutes
seconds
milliseconds
microseconds
nanoseconds
隐式转换为nanoseconds
,并且不会涉及任何截断或舍入错误。 只要您将溢出保持在两条明亮的白线内(模糊的参考将汽车保持在自己的车道上),就不会发生溢出。 只要持续时间在 +/- 292 年以内,您就不必担心这些预定义单位会溢出。
std 定义的函数(如 std::this_thread::sleep_for
)被模板化,正如 sehe 所建议的那样,正是出于希望与每个可以想象chrono:duration
互操作的原因(例如浮点飞秒的 1/3)。 由 API 设计者决定他们是否需要在自己的 API 中具有如此大的灵活性。
如果我现在设法让你感到困惑而不是澄清,请不要太担心。 如果选择使用 nanoseconds
,则事情将正常工作,没有截断或舍入错误,或者客户端将收到编译时错误。 不会有运行时错误。
void my_function(bool work_really_hard, std::chrono::nanoseconds timeout)
{
std::cout << timeout.count() << 'n';
}
int
main()
{
using namespace std;
using namespace std::chrono;
typedef duration<double, pico> picoseconds;
my_function(true, picoseconds(100000.25));
}
test.cpp:15:9: error: no matching function for call to 'my_function'
my_function(true, picoseconds(100000.25));
^~~~~~~~~~~
test.cpp:4:10: note: candidate function not viable: no known conversion from 'duration<double, ratio<[...], 1000000000000>>' to
'duration<long long, ratio<[...], 1000000000>>' for 2nd argument
void my_function(bool work_really_hard, std::chrono::nanoseconds timeout)
^
1 error generated.
如果客户端收到编译时错误,他总是可以使用duration_cast
来解决它:
my_function(true, duration_cast<nanoseconds>(picoseconds(100000.25))); // Ok
有关更多详细信息,请参阅:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm
对本答案顶部的原始代码进行甜蜜更新。 在 C++1y 中,我们希望这意味着 C++14:
using namespace std::literals;
my_function(true, 10min); // also ok, is equal to 600000000000 nanoseconds
问题:您建议什么作为"无穷大"超时(即不超时)
我将首先尝试使用没有超时的 API,并且暗示"不会超时"。 例如condition_variable::wait
. 如果我控制了 API,我会创建这样的签名而不会超时。
如果做不到这一点,我将创建一个系列的"大"chrono::durations
:
using days = std::chrono::duration
<
std::int32_t, std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>
>;
using weeks = std::chrono::duration
<
std::int32_t, std::ratio_multiply<days::period, std::ratio<7>>
>;
using years = std::chrono::duration
<
std::int32_t, std::ratio_multiply<days::period, std::ratio<146097, 400>>
>;
using months = std::chrono::duration
<
std::int32_t, std::ratio_divide<years::period, std::ratio<12>>
>;
然后,我会在通话中使用以下大持续时间之一,例如:
std::this_thread::sleep_for(years(3));
我不会尝试最大限度地push_things(例如,您可能会破坏操作系统所做的基本假设)。 只是任意挑选一些大得离谱的东西(比如 3 年)。 这将引起代码审阅者的注意,并可能引发信息丰富的对话。:-)
现在提供视频:https://www.youtube.com/watch?v=P32hvk8b13M :-)
使用模板参数作为超时类型,并期望std::chrono
类型之一,或具有类似接口的内容;这将允许您传入任何时间单位。
template<typename T>
void wait_a_while(const T& duration)
{
std::this_thread::sleep(duration);
}
wait_a_while(boost::posix_time::seconds(5));
wait_a_while(std::chrono::milliseconds(30));
- 静态自动 constexpr t = { "red" , "black" , "green" } 是什么类型;派生到?
- 这是什么类型的C++语法,我应该采取什么步骤来理解这一点
- 当你只使用 return 时,函数返回什么类型;在 c++ 中
- 这在C++是什么类型的错误?
- 如果在 C++ 构造函数中以错误的顺序初始化对象数据,会发生什么类型的错误
- 重载运算符返回什么类型的值(对于用户定义的类型):右值还是左值?
- int(int)& 或 int(int) const &是什么类型?
- 它>首先给出什么类型?
- ``n = 1u;'有什么类型
- 访问和存储/解析性std :: Chrono ::持续时间:: milliseconds(cpprest)时使用什么类型
- 二进制要找到什么类型的序列化
- 我如何找出3D容器的元素是什么类型
- C++ - 函数中的局部指针变量具有什么类型的存储持续时间以及它们存储在哪里?
- 将 lambda 作为模板参数传递:实际推导出什么类型
- 在 c 和 c++ 中,二维数组的元素是什么类型?
- 什么类型会使"std::has_unique_object_representations"返回 false?
- 这个 lambda 的参数是什么类型?
- 转换逻辑目标是什么类型
- 由值持有的成员的get函数应该具有什么类型
- C++中使用什么类型来定义数组大小