如果date是2116年,boost timed_wait不会等待

这个例子:

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

但是,如果我将系统date移动到2116年,并重新运行应用程序, timed_wait调用立即返回false 。 助推器的状态variables也有同样的问题。

可以请任何人解释它有什么问题吗?

信息:

  • Linux sles12 3.12.39 x86_64 GNU / Linux
  • g ++ 4.8
  • 提升1.54

UPD正如我已经写在上面,我正在64位平台上运行testing,并且time_t的大小等于8.所以build议使用64位time_t而不是32位的答案不适用于该问题。

让我试着描述一个小的堆栈跟踪到发生问题的线路(顺便说一句,我使用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 (持续时间以秒为单位,类型为time_t), tv_nsec (类型为long)等成员。

问题简单的是以秒为单位的持续时间(从01/01/1970开始到XX / XX / 2116)太大,不适合tv_sec (time_t基本上是一个有符号整数)。 这个问题也在这里描述: https : //stackoverflow.com/a/471287/5967798

我在2116年测试了它,函数返回的timespec描述了日期9:18:59 am CET | Wednesday, October 8, 1980 9:18:59 am CET | Wednesday, October 8, 1980

问题的根本原因是boost::posix_time::time_duration::total_seconds()明确地将返回值转换为time_resolution_traits::sec_type同时通过boost::interprocess::ipcdetail::ptime_to_timespec()ptime转换为timespec timed_wait()调用。 time_resolution_traits::sec_typeint32_t类型的typedef(请参阅boost / date_time / time_resolution_traits.hpp文件中的typedef链)。

同时,文档说time_duration::total_seconds()返回longhttp://www.boost.org/doc/libs/1_62_0/doc/html/date_time/posix_time.html )。 但是,对于LP64模型(例如Linux,64位),它将返回带符号的32位整数,不会long (带符号的64位整数)。

所以显然有一个提升的错误。 该错误的第一张票是在2009年创建的,而且还没有修复。

门票: