]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
Support for jthread and stop_token
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / stop_token / stop_token.cc
1 // Copyright (C) 2019 Free Software Foundation, Inc.
2 //
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)
7 // any later version.
8
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.
13
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/>.
17
18 // { dg-options "-std=gnu++2a -pthread" }
19 // { dg-do run }
20 // { dg-require-effective-target c++2a }
21 // { dg-require-effective-target pthread }
22
23 #include <stop_token>
24 #include <iostream>
25 #include <testsuite_hooks.h>
26
27 int main()
28 {
29 // create stop_source
30 std::stop_source ssrc;
31 VERIFY(ssrc.stop_possible());
32 VERIFY(!ssrc.stop_requested());
33
34 // create stop_token from stop_source
35 std::stop_token stok{ssrc.get_token()};
36 VERIFY(ssrc.stop_possible());
37 VERIFY(!ssrc.stop_requested());
38 VERIFY(stok.stop_possible());
39 VERIFY(!stok.stop_requested());
40
41 // register callback
42 bool cb1called{false};
43 auto cb1 = [&]{
44 std::cout << "cb1" << std::endl;
45 cb1called = true;
46 };
47 {
48 std::stop_callback scb1{stok, cb1};
49 VERIFY(ssrc.stop_possible());
50 VERIFY(!ssrc.stop_requested());
51 VERIFY(stok.stop_possible());
52 VERIFY(!stok.stop_requested());
53 VERIFY(!cb1called);
54 } // unregister callback
55
56 // register another callback
57 bool cb2called{false};
58 auto cb2 = [&]{
59 VERIFY(stok.stop_requested());
60 cb2called = true;
61 };
62 std::stop_callback scb2a{stok, cb2}; // copies cb2
63 // std::stop_callback scb2b{stok, std::move(cb2)};
64 VERIFY(ssrc.stop_possible());
65 VERIFY(!ssrc.stop_requested());
66 VERIFY(stok.stop_possible());
67 VERIFY(!stok.stop_requested());
68 VERIFY(!cb1called);
69 VERIFY(!cb2called);
70
71 // request stop
72 auto b = ssrc.request_stop();
73 VERIFY(b);
74 VERIFY(ssrc.stop_possible());
75 VERIFY(ssrc.stop_requested());
76 VERIFY(stok.stop_possible());
77 VERIFY(stok.stop_requested());
78 VERIFY(!cb1called);
79 VERIFY(cb2called);
80
81 b = ssrc.request_stop();
82 VERIFY(!b);
83
84 // TODO verify the standard requires this
85 #if 0
86 // register another callback
87 bool cb3called{false};
88 std::stop_callback scb3{stok, [&]
89 {
90 cb3called = true;
91 }};
92 VERIFY(ssrc.stop_possible());
93 VERIFY(ssrc.stop_requested());
94 VERIFY(stok.stop_possible());
95 VERIFY(stok.stop_requested());
96 VERIFY(!cb1called);
97 VERIFY(cb2called);
98 VERIFY(cb3called);
99 #endif
100 }