]>
Commit | Line | Data |
---|---|---|
75a8a745 | 1 | // { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } } |
4415f7a5 PC |
2 | // { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } } |
3 | // { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } } | |
4 | // { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } } | |
5d020aa2 JW |
5 | // { dg-require-cstdint "" } |
6 | // { dg-require-gthreads "" } | |
28c2f60e | 7 | // { dg-require-sched-yield "" } |
5d020aa2 | 8 | |
5624e564 | 9 | // Copyright (C) 2011-2015 Free Software Foundation, Inc. |
5d020aa2 JW |
10 | // |
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) | |
15 | // any later version. | |
16 | ||
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. | |
21 | ||
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/>. | |
25 | ||
26 | #include <condition_variable> | |
27 | #include <thread> | |
28 | #include <mutex> | |
29 | #include <array> | |
30 | #include <sstream> | |
31 | ||
32 | struct scoped_thread | |
33 | { | |
34 | ~scoped_thread() { if (t.joinable()) t.join(); } | |
35 | std::thread t; | |
36 | }; | |
37 | ||
38 | int main() | |
39 | { | |
40 | typedef std::unique_lock<std::mutex> Lock; | |
41 | ||
42 | std::mutex m; | |
43 | std::condition_variable_any cond; | |
e8a25ac8 PC |
44 | unsigned int product = 0; |
45 | const unsigned int count = 10; | |
5d020aa2 JW |
46 | |
47 | // writing to stream causes timing changes which makes deadlock easier | |
48 | // to reproduce - do not remove | |
49 | std::ostringstream out; | |
50 | ||
51 | // create consumers | |
52 | std::array<scoped_thread, 2> threads; | |
e8a25ac8 PC |
53 | for (std::size_t i = 0; i < threads.size(); ++i) |
54 | threads[i].t | |
55 | = std::thread( [&] | |
56 | { | |
57 | for (unsigned int i = 0; i < count; ++i) | |
58 | { | |
59 | std::this_thread::yield(); | |
60 | Lock lock(m); | |
61 | while(product == 0) | |
62 | cond.wait(lock); | |
63 | out << "got product " | |
64 | << std::this_thread::get_id() | |
65 | << ' ' << product << std::endl; | |
66 | --product; | |
67 | } | |
68 | } ); | |
5d020aa2 JW |
69 | |
70 | // single producer | |
e8a25ac8 PC |
71 | for (std::size_t i = 0; i < threads.size() * count; ++i) |
72 | { | |
73 | std::this_thread::yield(); | |
74 | Lock lock(m); | |
75 | ++product; | |
76 | out << "setting product " << std::this_thread::get_id() | |
77 | << ' ' << product << std::endl; | |
78 | cond.notify_one(); | |
79 | } | |
5d020aa2 | 80 | } |