]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/stop_token/stop_token.cc
1 // Copyright (C) 2019 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/>.
18 // { dg-options "-std=gnu++2a -pthread" }
20 // { dg-require-effective-target c++2a }
21 // { dg-require-effective-target pthread }
25 #include <testsuite_hooks.h>
30 std::stop_source ssrc
;
31 VERIFY(ssrc
.stop_possible());
32 VERIFY(!ssrc
.stop_requested());
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());
42 bool cb1called
{false};
44 std::cout
<< "cb1" << std::endl
;
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());
54 } // unregister callback
56 // register another callback
57 bool cb2called
{false};
59 VERIFY(stok
.stop_requested());
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());
72 auto b
= ssrc
.request_stop();
74 VERIFY(ssrc
.stop_possible());
75 VERIFY(ssrc
.stop_requested());
76 VERIFY(stok
.stop_possible());
77 VERIFY(stok
.stop_requested());
81 b
= ssrc
.request_stop();
84 // TODO verify the standard requires this
86 // register another callback
87 bool cb3called
{false};
88 std::stop_callback scb3
{stok
, [&]
92 VERIFY(ssrc
.stop_possible());
93 VERIFY(ssrc
.stop_requested());
94 VERIFY(stok
.stop_possible());
95 VERIFY(stok
.stop_requested());