Support multiple lock attributes of the same kind on a declaration.
[official-gcc.git] / libstdc++-v3 / testsuite / thread / pthread4.cc
blob146d8c7a59401c199835d083db881ef0e93996a1
1 // 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 // Adapted from http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html
3 // which was adapted from pthread1.cc by Mike Lu <MLu@dynamicsoft.com>
4 //
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
6 // 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 2, or (at your option)
12 // any later version.
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.
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING. If not, write to the Free
21 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // USA.
24 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
25 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
26 // { dg-options "-pthreads" { target *-*-solaris* } }
28 #include <string>
29 #include <list>
30 #include <pthread.h>
32 using namespace std;
34 static list<string> foo;
35 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
36 static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER;
37 static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER;
38 static unsigned max_size = 10;
39 #if defined(__CYGWIN__)
40 static int iters = 10000;
41 #else
42 static int iters = 300000;
43 #endif
45 void*
46 produce (void*)
48 for (int num = 0; num < iters; )
50 string str ("test string");
52 pthread_mutex_lock (&fooLock);
53 while (foo.size () >= max_size)
54 pthread_cond_wait (&fooCondOverflow, &fooLock);
55 foo.push_back (str);
56 num++;
57 if (foo.size () >= (max_size / 2))
58 pthread_cond_signal (&fooCondUnderflow);
59 pthread_mutex_unlock (&fooLock);
62 // No more data will ever be written, ensure no fini race
63 pthread_mutex_lock (&fooLock);
64 pthread_cond_signal (&fooCondUnderflow);
65 pthread_mutex_unlock (&fooLock);
67 return 0;
70 void*
71 consume (void*)
73 for (int num = 0; num < iters; )
75 pthread_mutex_lock (&fooLock);
76 while (foo.size () == 0)
77 pthread_cond_wait (&fooCondUnderflow, &fooLock);
78 while (foo.size () > 0)
80 string str = foo.back ();
81 foo.pop_back ();
82 num++;
84 pthread_cond_signal (&fooCondOverflow);
85 pthread_mutex_unlock (&fooLock);
88 return 0;
91 int
92 main (void)
94 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
95 pthread_setconcurrency (2);
96 #endif
98 pthread_t prod;
99 pthread_create (&prod, NULL, produce, NULL);
100 pthread_t cons;
101 pthread_create (&cons, NULL, consume, NULL);
103 pthread_join (prod, NULL);
104 pthread_join (cons, NULL);
106 return 0;