| 1 | // Copyright (C) 2019-2025 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-do run } |
| 19 | // { dg-options "-pthread" } |
| 20 | // { dg-require-effective-target c++11 } |
| 21 | // { dg-require-effective-target pthread } |
| 22 | // { dg-require-gthreads "" } |
| 23 | |
| 24 | #include <future> |
| 25 | #include <testsuite_rvalref.h> |
| 26 | |
| 27 | struct F : __gnu_test::copycounter |
| 28 | { |
| 29 | F() = default; |
| 30 | |
| 31 | F(const F&) = default; |
| 32 | |
| 33 | // Move constructor copies base class, to use counter: |
| 34 | F(F&& f) : copycounter(f) { f.valid = false; } |
| 35 | |
| 36 | void run() { VERIFY(this->valid); } |
| 37 | }; |
| 38 | |
| 39 | void |
| 40 | test01() |
| 41 | { |
| 42 | std::future<void> fut; |
| 43 | |
| 44 | F::copycount = 0; |
| 45 | fut = std::async(&F::run, F{}); |
| 46 | VERIFY( F::copycount == 1 ); |
| 47 | fut.get(); |
| 48 | VERIFY( F::copycount == 1 ); |
| 49 | |
| 50 | F::copycount = 0; |
| 51 | fut = std::async(std::launch::async, &F::run, F{}); |
| 52 | VERIFY( F::copycount == 1 ); |
| 53 | fut.get(); |
| 54 | VERIFY( F::copycount == 1 ); |
| 55 | |
| 56 | F::copycount = 0; |
| 57 | fut = std::async(std::launch::deferred, &F::run, F{}); |
| 58 | VERIFY( F::copycount == 1 ); |
| 59 | fut.get(); |
| 60 | VERIFY( F::copycount == 1 ); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | test02() |
| 65 | { |
| 66 | std::future<void> fut; |
| 67 | const F f; |
| 68 | |
| 69 | F::copycount = 0; |
| 70 | fut = std::async(&F::run, f); |
| 71 | VERIFY( F::copycount == 1 ); |
| 72 | fut.get(); |
| 73 | VERIFY( F::copycount == 1 ); |
| 74 | |
| 75 | F::copycount = 0; |
| 76 | fut = std::async(std::launch::async, &F::run, f); |
| 77 | VERIFY( F::copycount == 1 ); |
| 78 | fut.get(); |
| 79 | VERIFY( F::copycount == 1 ); |
| 80 | |
| 81 | F::copycount = 0; |
| 82 | fut = std::async(std::launch::deferred, &F::run, f); |
| 83 | VERIFY( F::copycount == 1 ); |
| 84 | fut.get(); |
| 85 | VERIFY( F::copycount == 1 ); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | test03() |
| 90 | { |
| 91 | std::future<void> fut; |
| 92 | F f; |
| 93 | |
| 94 | F::copycount = 0; |
| 95 | fut = std::async(&F::run, std::ref(f)); |
| 96 | VERIFY( F::copycount == 0 ); |
| 97 | fut.get(); |
| 98 | VERIFY( F::copycount == 0 ); |
| 99 | |
| 100 | F::copycount = 0; |
| 101 | fut = std::async(std::launch::async, &F::run, std::ref(f)); |
| 102 | VERIFY( F::copycount == 0 ); |
| 103 | fut.get(); |
| 104 | VERIFY( F::copycount == 0 ); |
| 105 | |
| 106 | F::copycount = 0; |
| 107 | fut = std::async(std::launch::deferred, &F::run, std::ref(f)); |
| 108 | VERIFY( F::copycount == 0 ); |
| 109 | fut.get(); |
| 110 | VERIFY( F::copycount == 0 ); |
| 111 | } |
| 112 | |
| 113 | int |
| 114 | main() |
| 115 | { |
| 116 | test01(); |
| 117 | test02(); |
| 118 | test03(); |
| 119 | } |