]>
Commit | Line | Data |
---|---|---|
ddd69607 LR |
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 | // | |
8fc81078 | 5 | // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010 |
1769232d | 6 | // Free Software Foundation, Inc. |
ddd69607 LR |
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 | |
748086b7 | 11 | // Free Software Foundation; either version 3, or (at your option) |
ddd69607 LR |
12 | // any later version. |
13 | // | |
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. | |
18 | // | |
19 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
20 | // with this library; see the file COPYING3. If not see |
21 | // <http://www.gnu.org/licenses/>. | |
ddd69607 | 22 | |
40f5cc95 RO |
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* } } | |
ddd69607 LR |
25 | // { dg-options "-pthreads" { target *-*-solaris* } } |
26 | ||
27 | #include <string> | |
28 | #include <list> | |
1769232d | 29 | #include <pthread.h> |
ddd69607 | 30 | |
ddd69607 LR |
31 | using namespace std; |
32 | ||
33 | static list<string> foo; | |
34 | static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER; | |
b625fdb7 LR |
35 | static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER; |
36 | static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER; | |
ddd69607 | 37 | static unsigned max_size = 10; |
a68d024d DB |
38 | #if defined(__CYGWIN__) |
39 | static int iters = 10000; | |
40 | #else | |
56d4fe31 | 41 | static int iters = 300000; |
a68d024d | 42 | #endif |
ddd69607 LR |
43 | |
44 | void* | |
45 | produce (void*) | |
46 | { | |
47 | for (int num = 0; num < iters; ) | |
48 | { | |
49 | string str ("test string"); | |
50 | ||
51 | pthread_mutex_lock (&fooLock); | |
b625fdb7 LR |
52 | while (foo.size () >= max_size) |
53 | pthread_cond_wait (&fooCondOverflow, &fooLock); | |
54 | foo.push_back (str); | |
55 | num++; | |
56 | if (foo.size () >= (max_size / 2)) | |
57 | pthread_cond_signal (&fooCondUnderflow); | |
ddd69607 LR |
58 | pthread_mutex_unlock (&fooLock); |
59 | } | |
60 | ||
07d75182 LR |
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); | |
65 | ||
ddd69607 LR |
66 | return 0; |
67 | } | |
68 | ||
69 | void* | |
70 | consume (void*) | |
71 | { | |
72 | for (int num = 0; num < iters; ) | |
73 | { | |
74 | pthread_mutex_lock (&fooLock); | |
b625fdb7 LR |
75 | while (foo.size () == 0) |
76 | pthread_cond_wait (&fooCondUnderflow, &fooLock); | |
ddd69607 LR |
77 | while (foo.size () > 0) |
78 | { | |
79 | string str = foo.back (); | |
80 | foo.pop_back (); | |
81 | num++; | |
82 | } | |
b625fdb7 | 83 | pthread_cond_signal (&fooCondOverflow); |
ddd69607 LR |
84 | pthread_mutex_unlock (&fooLock); |
85 | } | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | int | |
91 | main (void) | |
92 | { | |
87bd0274 | 93 | #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 |
ddd69607 LR |
94 | pthread_setconcurrency (2); |
95 | #endif | |
96 | ||
97 | pthread_t prod; | |
8fc81078 | 98 | pthread_create (&prod, 0, produce, 0); |
ddd69607 | 99 | pthread_t cons; |
8fc81078 | 100 | pthread_create (&cons, 0, consume, 0); |
ddd69607 | 101 | |
8fc81078 PC |
102 | pthread_join (prod, 0); |
103 | pthread_join (cons, 0); | |
ddd69607 LR |
104 | |
105 | return 0; | |
106 | } |