实现线程安全的ctime函数 [英] Implement thread safe ctime function

查看:385
本文介绍了实现线程安全的ctime函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ANSI C时间函数不是线程安全的:尤其是gmtimectimestrftime.据我所知,唯一的线程安全功能是time.

ANSI C time function are not thread safe: in particular gmtime, ctime and strftime. For what I know the only thread-safe function is time.

如果我对Visual Studio正确,那么此函数都是线程安全的.我正在使用MinGW编译器(带有dwarf-2的4.8.0,由Qt 5.1.0提供的那个).

If I'm right in Visual Studio this function are all thread safe. I'm using MinGW compiler (4.8.0 with dwarf-2, that one provided by Qt 5.1.0 ).

如何补充gmtime_rctime_rstrftime_r的线程安全功能?还是有一个更线程安全的库?

How can inplement thread safe function of gmtime_r, ctime_r and strftime_r? Or is there a more thread-safe library for that?

我使用C ++ 11作为默认语言.

I'm using C++11 as default language.

修改1
我在Windows XP上使用的是MinGW 4.8.0(posix,dwarf-2).

edit 1
I'm using MinGW 4.8.0 (posix, dwarf-2) on windows xp.

修改2
我正在考虑使用Boost.DateTime,这可能是个不错的选择吗?

edit 2
I'm considering to use Boost.DateTime may it be a good choice?

我对缺少c ++ 11的时间管理感到非常失望!

I'm very disappointed for the lack on c++11 of time managment!

推荐答案

pthread.h中实际上应该有gmtime_rctime_r,但是由于存在错误,它们被临时删除了(

There actually should be gmtime_r and ctime_r in pthread.h but they are temporary removed because of a bug (http://sourceforge.net/p/mingw/bugs/1625/).

您别无选择

  1. 等待,直到将其修复
  2. 使用旧版的MiniGW
  3. 使用lock_guard为所有这些功能创建可重入包装,就像safe_increment完成
  1. Wait until it will be fixed
  2. Use older version of MiniGW
  3. Make a reentrant wrapper for all of those functions using lock_guard just like safe_increment has done here
  4. And maybe best way is to use functions form chrono header file added by C++11

这篇关于实现线程安全的ctime函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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