为什么没有C ++ 11线程安全替代std :: localtime和std :: gmtime? [英] Why is there no C++11 threadsafe alternative to std::localtime and std::gmtime?

查看:308
本文介绍了为什么没有C ++ 11线程安全替代std :: localtime和std :: gmtime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,您仍然必须使用 std :: localtime std :: gmtime 作为间接打印 std :: chrono :: time_point 。这些函数在C ++ 11引入的多线程环境中不能安全使用,因为它们会返回指向内部静态结构的指针。由于C ++ 11引入了方便的功能 std :: put_time ,由于相同的原因,该功能几乎无法使用,这特别令人讨厌。

In C++11 you still have to use std::localtime and std::gmtime as indirection to print a std::chrono::time_point. These functions are not safe to use in a multithreaded environment as introduced with C++11 because they return a pointer to an internal static struct. This is especially annoying since C++11 introduced the convenient function std::put_time which is nearly unusable for the same reason.

为什么这么根本性的破坏或我忽略了什么?

Why is this so fundamental broken or do I overlook something?

推荐答案

根据N2661 ,该文件添加了< chrono>


本文仅提供往返于C的<$ c的最小
,不提供日历服务。 $ c> time_t 。


由于本文没有提出日期/时间库,也没有指定历元,它也不能解决leap秒问题。但是,日期/时间
库将发现它是构建
的绝佳基础。

As this paper does not propose a date/time library, nor specify epochs, it also does not address leap seconds. However, a date/time library will find this to be an excellent foundation on which to build.

本文没有提出通用的物理量
库。

This paper does not propose a general purpose physical quantities library.


本文提出了坚实的基础,将来,可以为通用物理单位
库提供兼容的起点。尽管这样的未来图书馆可能会采用多种形式,但是
的提议在实际成为实体
单位之前远远不够。这项建议是针对特定时间的,并且继续受线程库与时间相关的需求的激励。

This paper proposes a solid foundation that, in the future, could provide a compatible starting point for a general physical units library. While such a future library might take any of several forms, the present proposal stops well short of actually being a physical units library. This proposal is time-specific, and continues to be motivated by the time-related needs of the threading library.

该提案的主要目标是以一种易于使用,安全使用
,高效且灵活到不被淘汰10甚至什至是的方式满足
标准库线程API的需求。
从现在开始100年了。出于特定原因,此处以实际用例为动机,为此提案中包含的每个功能都是
。属于
的事物属于酷类别,或者听起来像
有用,或者非常有用,但此接口不需要,因此未包含
。此类项目可能会出现在其他提案中,并且
可能定位到TR。

The major goal of this proposal is to satisfy the needs of the standard library threading API in a manner which is easy to use, safe to use, efficient, and flexible enough to not be obsolete 10 or even 100 years from now. Every feature contained in this proposal is here for a specific reason with practical use cases as motivation. Things that fell into the category of "cool", or "that sounds like it might be useful", or "very useful but not needed by this interface" have not been included. Such items might appear in other proposals, and possibly target a TR.

请注意,<$ c的主要目标$ c>< chrono> 是满足标准库线程API的需求,不需要日历服务。

Note that the major goal of <chrono> is "to satisfy the needs of the standard library threading API", which does not require calendar services.

这篇关于为什么没有C ++ 11线程安全替代std :: localtime和std :: gmtime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆