From: Igor Shevlyakov <[email protected]>

test_pr91486_wait_for and test_pr91486_wait_until capture start_steady
after launching the async task whose 1s sleep they wait on.  On a heavily
loaded machine the interval between launching the task and capturing
start_steady can be large enough that the future becomes ready (and
wait_for or wait_until returns) less than 1s after start_steady,
spuriously aborting on the 'elapsed_steady >= 1s' assertion.

Capture start_steady before launching the task so the measured interval
fully contains the task's sleep.

        PR libstdc++/91486

libstdc++-v3/ChangeLog:

        * testsuite/30_threads/async/async.cc (test_pr91486_wait_for)
        (test_pr91486_wait_until): Capture start time before launching
        the async task.

Signed-off-by: Igor Shevlyakov <[email protected]>
---
async.cc run on x86_64-pc-linux-gnu with and without an injected 300ms delay 
after the async launch: v1 fails in test_pr91486_wait_for, v2 passes

Changes since v1:
- test_pr91486_wait_for has the same race as test_pr91486_wait_until
  (start_steady is captured after the async launch); capture it before
  the launch there too.  Pointed out by the Sashiko review of v1.

 libstdc++-v3/testsuite/30_threads/async/async.cc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/testsuite/30_threads/async/async.cc 
b/libstdc++-v3/testsuite/30_threads/async/async.cc
index b49d2b28b57..1efb1fefeef 100644
--- a/libstdc++-v3/testsuite/30_threads/async/async.cc
+++ b/libstdc++-v3/testsuite/30_threads/async/async.cc
@@ -167,12 +167,15 @@ void test04()
 
 void test_pr91486_wait_for()
 {
+  // Capture the start time before launching the task, so that the
+  // measured interval contains its whole 1s sleep.
+  auto const start_steady = chrono::steady_clock::now();
+
   future<void> f1 = async(launch::async, []() {
       std::this_thread::sleep_for(std::chrono::seconds(1));
     });
 
   std::chrono::duration<float> const wait_time = std::chrono::seconds(1);
-  auto const start_steady = chrono::steady_clock::now();
   auto status __attribute__ ((__unused__)) = f1.wait_for(wait_time);
   auto const elapsed_steady = chrono::steady_clock::now() - start_steady;
 
@@ -214,6 +217,10 @@ int float_steady_clock::call_count = 0;
 
 void test_pr91486_wait_until()
 {
+  // Capture the start time before launching the task, so that the
+  // measured interval contains its whole 1s sleep.
+  auto const start_steady = chrono::steady_clock::now();
+
   future<void> f1 = async(launch::async, []() {
       std::this_thread::sleep_for(std::chrono::seconds(1));
     });
@@ -237,7 +244,6 @@ void test_pr91486_wait_until()
   float_steady_clock::time_point const expire = now + wait_time;
   VERIFY( expire > now );
 
-  auto const start_steady = chrono::steady_clock::now();
   auto status __attribute__ ((__unused__)) = f1.wait_until(expire);
   auto const elapsed_steady = chrono::steady_clock::now() - start_steady;
 
-- 
2.55.0

Reply via email to