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>
5 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009
6 // Free Software Foundation, Inc.
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)
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 COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
33 static list
<string
> foo
;
34 static pthread_mutex_t fooLock
= PTHREAD_MUTEX_INITIALIZER
;
35 static pthread_cond_t fooCondOverflow
= PTHREAD_COND_INITIALIZER
;
36 static pthread_cond_t fooCondUnderflow
= PTHREAD_COND_INITIALIZER
;
37 static unsigned max_size
= 10;
38 #if defined(__CYGWIN__)
39 static int iters
= 10000;
41 static int iters
= 300000;
47 for (int num
= 0; num
< iters
; )
49 string
str ("test string");
51 pthread_mutex_lock (&fooLock
);
52 while (foo
.size () >= max_size
)
53 pthread_cond_wait (&fooCondOverflow
, &fooLock
);
56 if (foo
.size () >= (max_size
/ 2))
57 pthread_cond_signal (&fooCondUnderflow
);
58 pthread_mutex_unlock (&fooLock
);
61 // No more data will ever be written, ensure no fini race
62 pthread_mutex_lock (&fooLock
);
63 pthread_cond_signal (&fooCondUnderflow
);
64 pthread_mutex_unlock (&fooLock
);
72 for (int num
= 0; num
< iters
; )
74 pthread_mutex_lock (&fooLock
);
75 while (foo
.size () == 0)
76 pthread_cond_wait (&fooCondUnderflow
, &fooLock
);
77 while (foo
.size () > 0)
79 string str
= foo
.back ();
83 pthread_cond_signal (&fooCondOverflow
);
84 pthread_mutex_unlock (&fooLock
);
93 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
94 pthread_setconcurrency (2);
98 pthread_create (&prod
, NULL
, produce
, NULL
);
100 pthread_create (&cons
, NULL
, consume
, NULL
);
102 pthread_join (prod
, NULL
);
103 pthread_join (cons
, NULL
);