| 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) 2009-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 <thread> |
| 25 |
#include <system_error> |
| 26 |
#include <testsuite_hooks.h> |
| 27 |
|
| 28 |
int total = 0; |
| 29 |
|
| 30 |
// Functor has internal state. |
| 31 |
struct moveable |
| 32 |
{ |
| 33 |
int i; |
| 34 |
|
| 35 |
moveable() = default; |
| 36 |
~moveable() = default; |
| 37 |
moveable(const moveable& c) = delete; |
| 38 |
moveable& operator=(const moveable&) = delete; |
| 39 |
|
| 40 |
moveable(int j): i(j) { } |
| 41 |
moveable(moveable&& m): i(m.i) { } |
| 42 |
|
| 43 |
void operator()() const { total += i; } |
| 44 |
}; |
| 45 |
|
| 46 |
// Two threads called by same functor type, different functor objects |
| 47 |
// that have different state. Make sure each thread calls the correct |
| 48 |
// functor. |
| 49 |
void test09() |
| 50 |
{ |
| 51 |
try |
| 52 |
{ |
| 53 |
// first |
| 54 |
moveable m1(60); |
| 55 |
std::thread t1(std::move(m1)); |
| 56 |
t1.join(); |
| 57 |
VERIFY( total == 60 ); |
| 58 |
|
| 59 |
// second |
| 60 |
moveable m2(600); |
| 61 |
std::thread t2(std::move(m2)); |
| 62 |
t2.join(); |
| 63 |
VERIFY( total == 660 ); // Not 120... |
| 64 |
} |
| 65 |
catch (const std::system_error&) |
| 66 |
{ |
| 67 |
VERIFY( false ); |
| 68 |
} |
| 69 |
catch (...) |
| 70 |
{ |
| 71 |
VERIFY( false ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
int main() |
| 76 |
{ |
| 77 |
test09(); |
| 78 |
return 0; |
| 79 |
} |