OSDN Git Service

b85a5e136266c585b09d2cf64e7ab4f5a2006158
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 30_threads / condition_variable_any / 50862.cc
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 "" }
8  
9 // Copyright (C) 2011 Free Software Foundation, Inc.
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;
44   unsigned int                product = 0;
45   const unsigned int          count = 10;
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;
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                      } );
69
70   // single producer
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     }
80 }