Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any std::chrono thread safety guarantee even with multicore context?

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?

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.

What I want to know is:

  1. using std::chrono, in the standard, is there any guarantee that think kind of problem shouldn't appear?
  2. or is it implementation defined
  3. or is there an explicit absence of guarantee that imply that on windows you'd better get time always from the same core?
like image 541
Klaim Avatar asked Jun 05 '12 09:06

Klaim


People also ask

Is std :: Chrono thread-safe?

After the initialization, the database will hold a single initialized std::chrono::tzdb object. This function is thread-safe: concurrent calls to this function from multiple threads do not introduce a data race.

Are C++ sets thread-safe?

It is safe to read and write to one instance of a type even if another thread is reading or writing to a different instance of the same type. For example, given objects A and B of the same type, it is safe if A is being written in thread 1 and B is being read in thread 2.

Is STD map read thread-safe?

Yes it is. Save this answer. Show activity on this post. The following thread safety rules apply to all classes in the C++ Standard Library—this includes shared_ptr, as described below.

What is thread-safe in CPP?

An object is thread-safe for reading from multiple threads. For example, given an object A, it is safe to read A from thread 1 and from thread 2 simultaneously. If an object is being written to by one thread, then all reads and writes to that object on the same or other threads must be protected.


1 Answers

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

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.

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.

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.

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.

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

like image 63
Anthony Williams Avatar answered Sep 20 '22 11:09

Anthony Williams