]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc
1 // Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
19 // { dg-additional-options "-pthread" { target pthread } }
20 // { dg-require-effective-target c++11 }
21 // { dg-require-gthreads "" }
23 #include <condition_variable>
24 #include <testsuite_hooks.h>
32 std::condition_variable cv
;
34 for (int i
= 0; i
< 3; ++i
)
36 std::unique_lock
<std::mutex
> l(mx
);
37 auto start
= std::chrono::system_clock::now();
38 cv
.wait_for(l
, std::chrono::duration
<float>(1), [] { return false; });
39 auto t
= std::chrono::system_clock::now();
40 VERIFY( (t
- start
) >= std::chrono::seconds(1) );
44 // In order to ensure that the delta calculated in the arbitrary clock overload
45 // of condition_variable::wait_until fits accurately in a float, but the result
46 // of adding it to steady_clock with a float duration does not, this clock
47 // needs to use a more recent epoch.
48 struct recent_epoch_float_clock
50 using duration
= std::chrono::duration
<float>;
51 using rep
= duration::rep
;
52 using period
= duration::period
;
54 = std::chrono::time_point
<recent_epoch_float_clock
, duration
>;
55 static constexpr bool is_steady
= true;
57 static const std::chrono::steady_clock::time_point epoch
;
59 static time_point
now()
61 const auto steady
= std::chrono::steady_clock::now();
62 return time_point
{steady
- epoch
};
66 const std::chrono::steady_clock::time_point
recent_epoch_float_clock::epoch
=
67 std::chrono::steady_clock::now();
72 using clock
= recent_epoch_float_clock
;
75 std::condition_variable cv
;
77 for (int i
= 0; i
< 3; ++i
)
79 std::unique_lock
<std::mutex
> l(mx
);
80 const auto start
= clock::now();
81 const auto wait_time
= start
+ std::chrono::duration
<float>{1.0};
83 // In theory we could get a spurious wakeup, but in practice we won't.
84 const auto result
= cv
.wait_until(l
, wait_time
);
86 VERIFY( result
== std::cv_status::timeout
);
87 const auto elapsed
= clock::now() - start
;
88 VERIFY( elapsed
>= std::chrono::seconds(1) );