Debian Codegraph @

libstdc++-v3/testsuite/30_threads/async/sync.cc

raw
1 // { dg-do run }
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
5
6 // Copyright (C) 2010-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23
24 #include <future>
25 #include <testsuite_hooks.h>
26
27 struct sum {
28 typedef int result_type;
29 int operator()(int i, int& j, const int& k) { return i + j + k; }
30 };
31
32 void test01()
33 {
34 using namespace std;
35
36 int a = 1;
37 int b = 1;
38 int c = 1;
39 future<int> f1 = async(launch::deferred, sum(), a, ref(b), cref(c));
40 a = 0;
41 b = 10;
42 c = 100;
43
44 const std::chrono::seconds delay(10);
45 const auto then = std::chrono::system_clock::now() + delay;
46
47 VERIFY( f1.valid() );
48 // timed waiting functions should return 'deferred' immediately
49 VERIFY( f1.wait_until(then) == std::future_status::deferred );
50 VERIFY( f1.wait_for(delay) == std::future_status::deferred );
51 VERIFY( std::chrono::system_clock::now() < then );
52
53 f1.wait();
54
55 VERIFY( f1.valid() );
56 // timed waiting functions should return 'ready' immediately
57 VERIFY( f1.wait_until(then) == std::future_status::ready );
58 VERIFY( f1.wait_for(delay) == std::future_status::ready );
59 VERIFY( std::chrono::system_clock::now() < then );
60
61 VERIFY( f1.get() == 111 );
62 VERIFY( !f1.valid() );
63 }
64
65 int main()
66 {
67 test01();
68 return 0;
69 }