1 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
2 // { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
3 // { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
4 // { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
5 // { dg-require-cstdint "" }
6 // { dg-require-gthreads "" }
7 // { dg-require-sched-yield "" }
9 // Copyright (C) 2011 Free Software Foundation, Inc.
11 // This file is part of the GNU ISO C++ Library. This library is free
12 // software; you can redistribute it and/or modify it under the
13 // terms of the GNU General Public License as published by the
14 // Free Software Foundation; either version 3, or (at your option)
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // You should have received a copy of the GNU General Public License along
23 // with this library; see the file COPYING3. If not see
24 // <http://www.gnu.org/licenses/>.
26 #include <condition_variable>
34 ~scoped_thread() { if (t.joinable()) t.join(); }
40 typedef std::unique_lock<std::mutex> Lock;
43 std::condition_variable_any cond;
44 unsigned int product = 0;
45 const unsigned int count = 10;
47 // writing to stream causes timing changes which makes deadlock easier
48 // to reproduce - do not remove
49 std::ostringstream out;
52 std::array<scoped_thread, 2> threads;
53 for (std::size_t i = 0; i < threads.size(); ++i)
57 for (unsigned int i = 0; i < count; ++i)
59 std::this_thread::yield();
64 << std::this_thread::get_id()
65 << ' ' << product << std::endl;
71 for (std::size_t i = 0; i < threads.size() * count; ++i)
73 std::this_thread::yield();
76 out << "setting product " << std::this_thread::get_id()
77 << ' ' << product << std::endl;