C++ boost ptime是否线程安全?
我试图找到一种线程安全的方法来获取本地时间。从boost示例中,我得到了以下结果:C++ boost ptime是否线程安全?,c++,datetime,boost,C++,Datetime,Boost,我试图找到一种线程安全的方法来获取本地时间。从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 f
#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/date\u time/posix\u time/posix\u time.hpp”
#包括
int
main()
{
使用名称空间boost::posix_time;
使用名称空间boost::gregorian;
//从时钟获取当前时间--1秒分辨率
ptime now=秒时钟::本地时钟();
//将日期部分从时间中取出
date today=now.date();
日期明天=今天+天(1);
p明天开始(明天);//午夜
//迭代器加1小时
时间迭代器titr(现在,小时(1));
对于(;titr std::cout是的,您的平台中有一个支持它:
Date time现在在定义BOOST_HAS_线程时支持它们的平台上使用可重入POSIX函数
从
BOOST_HAS_线程现在基本上一直都是定义的。如果您有疑问,可以检查您平台的POSIX支持。谢谢,但是如何知道BOOST_HAS_线程是否定义?我在Ubuntu上使用最新的BOOST和GCC/ClangAs,我说过,BOOST_HAS_线程已经定义。如果您想确定,只需检查一下(#ifdef BOOST_有线程
。)