Use steady_clock to implement condition_variable::wait_for
authorMike Crowe <mac@mcrowe.com>
Wed, 1 Aug 2018 15:39:57 +0000 (15:39 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 1 Aug 2018 15:39:57 +0000 (16:39 +0100)
The C++ standard says that std::condition_variable::wait_for should be
implemented to be equivalent to:

  return wait_until(lock, chrono::steady_clock::now() + rel_time);

But the existing implementation uses chrono::system_clock. Now that
wait_until has potentially-different behaviour for chrono::steady_clock,
let's at least try to wait using the correct clock.

2018-08-01  Mike Crowe  <mac@mcrowe.com>

* include/std/condition_variable (wait_for): Use steady_clock.

From-SVN: r263225

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/condition_variable

index dc4cc14153044e1becaa1d46f01aeae8885e1c71..71c01f94997a49ed8c09cab1bd866025cf771e45 100644 (file)
@@ -1,3 +1,7 @@
+2018-08-01  Mike Crowe  <mac@mcrowe.com>
+
+       * include/std/condition_variable (wait_for): Use steady_clock.
+
 2018-08-01  Mike Crowe  <mac@mcrowe.com>
 
        * include/std/condition_variable (wait_until): Only report timeout
index c00afa2b7ae2169f7ba81ade07544381a7ec050a..1f84ea324ebb9a5febdf1e867312b51c50a83b60 100644 (file)
@@ -66,6 +66,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   class condition_variable
   {
     typedef chrono::system_clock       __clock_t;
+    typedef chrono::steady_clock       __steady_clock_t;
     typedef __gthread_cond_t           __native_type;
 
 #ifdef __GTHREAD_COND_INIT
@@ -144,11 +145,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       wait_for(unique_lock<mutex>& __lock,
               const chrono::duration<_Rep, _Period>& __rtime)
       {
-       using __dur = typename __clock_t::duration;
+       using __dur = typename __steady_clock_t::duration;
        auto __reltime = chrono::duration_cast<__dur>(__rtime);
        if (__reltime < __rtime)
          ++__reltime;
-       return wait_until(__lock, __clock_t::now() + __reltime);
+       return wait_until(__lock, __steady_clock_t::now() + __reltime);
       }
 
     template<typename _Rep, typename _Period, typename _Predicate>