即使在多核上下文中也有std :: chrono线程安全性保证吗? [英] Is there any std::chrono thread safety guaranty even with multicore context?

查看:93
本文介绍了即使在多核上下文中也有std :: chrono线程安全性保证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我假设调用std :: chrono的任何函数都保证是线程安全的(没有未定义的行为或竞争条件,或者从不同线程进行调用也不会有任何危险)。我正确吗?

First, I'm assuming that calling any function of std::chrono is guaranteed to be thread-safe (no undefined behaviour or race conditions or anything dangerous if called from different threads). Am I correct?

下一步,例如,在Windows上与多核处理器有关的问题,迫使一些与时间相关的实现允许强制特定内核获取任何时间信息的系统。

Next, for example on windows there is a well known problem related to multi-core processors that force some implementations of time related systems to allow forcing a specific core to get any time information.

我想知道的是:


  1. 使用std :: chrono,在标准中,是否可以保证不会出现思考类问题?

  2. 还是它的实现

  3. 还是没有明确表示是否最好在Windows上始终从同一内核获取时间?


推荐答案

是的,从不同线程调用 some_clock :: now()应该是线程安全的。

Yes, calls to some_clock::now() from different threads should be thread safe.

关于您在 QueryPerformanceCounter 中提到的特定问题,仅仅是Windows API在某些平台上暴露了硬件问题。其他操作系统可能会也可能不会将此硬件问题暴露给用户代码。

As regards the specific issue you mention with QueryPerformanceCounter, it is just that the Windows API exposes a hardware issue on some platforms. Other OSes may or may not expose this hardware issue to user code.

就C ++标准而言,如果时钟声称是稳定时钟,则它绝不能向后退,因此,即使在同一线程上有两次读取,即使操作系统将线程切换到其他处理器,第二个线程也绝不能早于第一个线程返回值。

As far as the C++ standard is concerned, if the clock claims to be a "steady clock" then it must never go backwards, so if there are two reads on the same thread, the second must never return a value earlier than the first, even if the OS switches the thread to a different processor.

对于非稳定时钟(例如,在许多系统上,例如 std :: chrono :: system_clock ),由于外部代理,因此无法保证仍然可以任意更改时钟。

For non-steady clocks (such as std::chrono::system_clock on many systems), there is no guarantee about this, since an external agent could change the clock arbitrarily anyway.

使用我对C ++ 11线程库的实现(包括 std :: chrono 之类的东西)的实现要注意确保稳定的时钟确实稳定。除了确保对同步进行原始调用之外,对 QueryPerformanceCounter 的原始调用确实要付出一定的代价,但不再将线程固定到CPU 0(它过去曾这样做)。我希望其他实现也可以解决此问题。

With my implementation of the C++11 thread library (including the std::chrono stuff) the implementation takes care to ensure that the steady clocks are indeed steady. This does impose a cost over and above a raw call to QueryPerformanceCounter to ensure the synchronization, but no longer pins the thread to CPU 0 (which it used to do). I would expect other implementations to have workarounds for this issue too.

稳定时钟的要求在20.11.3 [time.clock.req]中(C ++ 11标准)

The requirements for a steady clock are in 20.11.3 [time.clock.req] (C++11 standard)

这篇关于即使在多核上下文中也有std :: chrono线程安全性保证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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