| 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) 2008-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 <functional> // std::ref |
| 25 | #include <thread> |
| 26 | #include <system_error> |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | struct copyable |
| 30 | { |
| 31 | copyable() = default; |
| 32 | ~copyable() = default; |
| 33 | copyable(const copyable& c) |
| 34 | { ++copy_count; } |
| 35 | |
| 36 | void operator()(std::thread::id& id) const |
| 37 | { |
| 38 | id = std::this_thread::get_id(); |
| 39 | } |
| 40 | |
| 41 | static int copy_count; |
| 42 | }; |
| 43 | |
| 44 | int copyable::copy_count = 0; |
| 45 | |
| 46 | // same as 2, but function is copyable function object |
| 47 | // thread variadic cons not copied when std::ref |
| 48 | // thread variadic cons copied when not std::ref |
| 49 | // no errors |
| 50 | void test03() |
| 51 | { |
| 52 | try |
| 53 | { |
| 54 | std::thread::id t1_id1; |
| 55 | copyable c1; |
| 56 | std::thread t1(std::ref(c1), std::ref(t1_id1)); |
| 57 | std::thread::id t1_id2 = t1.get_id(); |
| 58 | VERIFY( t1.joinable() ); |
| 59 | t1.join(); |
| 60 | VERIFY( !t1.joinable() ); |
| 61 | VERIFY( t1_id1 == t1_id2 ); |
| 62 | VERIFY( copyable::copy_count == 0 ); |
| 63 | |
| 64 | std::thread::id t2_id1; |
| 65 | copyable c2; |
| 66 | std::thread t2(c2, std::ref(t2_id1)); |
| 67 | std::thread::id t2_id2 = t2.get_id(); |
| 68 | VERIFY( t2.joinable() ); |
| 69 | t2.join(); |
| 70 | VERIFY( !t2.joinable() ); |
| 71 | VERIFY( t2_id1 == t2_id2 ); |
| 72 | VERIFY( copyable::copy_count > 0 ); |
| 73 | } |
| 74 | catch (const std::system_error&) |
| 75 | { |
| 76 | VERIFY( false ); |
| 77 | } |
| 78 | catch (...) |
| 79 | { |
| 80 | VERIFY( false ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int main() |
| 85 | { |
| 86 | test03(); |
| 87 | } |