gpt4 book ai didi

c++ - boost ptime 线程安全与否?

转载 作者:行者123 更新时间:2023-11-28 02:48:56 28 4
gpt4 key购买 nike

我试图找到一种线程安全的方法来获取本地时间。从 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;
}

但是不知道能不能用在多线程环境下?

最佳答案

是的,您的平台支持它:

Date-time now uses reentrant POSIX functions on those platforms that support them when BOOST_HAS_THREADS is defined.

来自 here

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

关于c++ - boost ptime 线程安全与否?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23499462/

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