gpt4 book ai didi

c++ - 如果日期在 2116 年,boost timed_wait 不会等待

转载 作者:太空狗 更新时间:2023-10-29 20:21:55 26 4
gpt4 key购买 nike

例子:

boost::interprocess::interprocess_semaphore semDone(0);
long sec = 10;
boost::posix_time::ptime until = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(sec);
bool ret = semDone.timed_wait(until);

当时它在 Linux 上完美运行 - timed_wait 等待 10 秒然后返回 false

但是,如果我将系统日期移动到 2116 年并重新运行应用程序,timed_wait 调用会立即返回 false。 boost 的条件变量也存在同样的问题。

谁能解释一下它有什么问题吗?

信息:

  • Linux sles12 3.12.39 x86_64 GNU/Linux
  • g++ 4.8
  • boost 1.54

UPD 正如我在上面写的那样,我在64 位平台 上运行测试,time_t 的大小等于 8。所以建议使用 64 位的答案 time_t 而不是 32bit 不适用于该问题。

最佳答案

让我试着描述一个小堆栈跟踪发生了什么问题发生的行(顺便说一句,我使用的是 boost 1.58):

#0  boost::interprocess::ipcdetail::ptime_to_timespec (tm=...) at /usr/include/boost/interprocess/sync/posix/ptime_to_timespec.hpp:38
#1 0x402983 in boost::interprocess::ipcdetail::semaphore_timed_wait (handle=0x7fffffffe100, abs_time=...) at /usr/include/boost/interprocess/sync/posix/semaphore_wrapper.hpp:224
#2 0x402ab7 in boost::interprocess::ipcdetail::posix_semaphore::timed_wait (this=0x7fffffffe100, abs_time=...) at /usr/include/boost/interprocess/sync/posix/semaphore.hpp:55
#3 0x402b1d in boost::interprocess::interprocess_semaphore::timed_wait (this=0x7fffffffe100, abs_time=...) at /usr/include/boost/interprocess/sync/interprocess_semaphore.hpp:139
#4 0x40184d in main () at /tmp/t.cpp:10

好的,让我们看看在方法 ptime_to_timespec 中发生了什么。您之前创建的 ptime 对象(第 3 行)

boost::posix_time::ptime until = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(sec);

传递给此函数并转换为 timespec 对象。 timespec 对象在 time.h 中定义,可用于计算自 1970 年 1 月 1 日 00:00:00 以来的持续时间。(参见 http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html)它包含像 tv_sec< 这样的成员/strong>(以秒为单位的持续时间,类型 time_t),tv_nsec(类型 long)等。

问题只是以秒为单位的持续时间(从 01/01/1970 开始到 XX/XX/2116 结束)太大,不适合 tv_sec(time_t 基本上是一个签名 int)。此处也描述了该问题:https://stackoverflow.com/a/471287/5967798

我用 2116 年测试了它,函数返回的 timespec 描述了日期欧洲中部时间上午 9:18:59 | 1980 年 10 月 8 日星期三

关于c++ - 如果日期在 2116 年,boost timed_wait 不会等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40590921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com