繁体   English   中英

boost ptime是否是线程安全的?

[英]boost ptime is thread safe or not?

我试图找到一种线程安全的方式来获取当地时间。 从boost示例中,我得到了这个:

#include "boost/date_time/posix_time/posix_time.hpp"
  #include <iostream>

  int
  main() 
  {
    using namespace boost::posix_time;
    using namespace boost::gregorian;

    //get the current time from the clock -- one second resolution
    ptime now = second_clock::local_time();
    //Get the date part out of the time
    date today = now.date();
    date tommorrow = today + days(1);
    ptime tommorrow_start(tommorrow); //midnight 

    //iterator adds by one hour
    time_iterator titr(now,hours(1)); 
    for (; titr < tommorrow_start; ++titr) {
      std::cout << to_simple_string(*titr) << std::endl;
    }

    time_duration remaining = tommorrow_start - now;
    std::cout << "Time left till midnight: " 
              << to_simple_string(remaining) << std::endl;
    return 0;
  }

但我不知道它是否可以用于多线程环境?

是的,您的平台支持它:

现在,在定义BOOST_HAS_THREADS时,日期时间在支持它们的平台上使用可重入的POSIX函数。

这里开始

BOOST_HAS_THREADS基本上总是定义这些天。 如果您有疑问,可以查看平台的POSIX支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM