]>
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 | // | |
83ffe9cd | 5 | // Copyright (C) 2002-2023 Free Software Foundation, Inc. |
ddd69607 LR |
6 | // |
7 | // This file is part of the GNU ISO C++ Library. This library is free | |
8 | // software; you can redistribute it and/or modify it under the | |
9 | // terms of the GNU General Public License as published by the | |
748086b7 | 10 | // Free Software Foundation; either version 3, or (at your option) |
ddd69607 LR |
11 | // any later version. |
12 | // | |
13 | // This library is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | // | |
18 | // You should have received a copy of the GNU General Public License along | |
748086b7 JJ |
19 | // with this library; see the file COPYING3. If not see |
20 | // <http://www.gnu.org/licenses/>. | |
ddd69607 | 21 | |
d1236680 RO |
22 | // { dg-do run } |
23 | // { dg-options "-pthread" } | |
24 | // { dg-require-effective-target pthread } | |
ddd69607 LR |
25 | |
26 | #include <string> | |
27 | #include <list> | |
1769232d | 28 | #include <pthread.h> |
ddd69607 | 29 | |
ddd69607 LR |
30 | using namespace std; |
31 | ||
32 | static list<string> foo; | |
33 | static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER; | |
b625fdb7 LR |
34 | static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER; |
35 | static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER; | |
ddd69607 | 36 | static unsigned max_size = 10; |
a68d024d DB |
37 | #if defined(__CYGWIN__) |
38 | static int iters = 10000; | |
39 | #else | |
56d4fe31 | 40 | static int iters = 300000; |
a68d024d | 41 | #endif |
ddd69607 LR |
42 | |
43 | void* | |
44 | produce (void*) | |
45 | { | |
46 | for (int num = 0; num < iters; ) | |
47 | { | |
48 | string str ("test string"); | |
49 | ||
50 | pthread_mutex_lock (&fooLock); | |
b625fdb7 LR |
51 | while (foo.size () >= max_size) |
52 | pthread_cond_wait (&fooCondOverflow, &fooLock); | |
53 | foo.push_back (str); | |
54 | num++; | |
55 | if (foo.size () >= (max_size / 2)) | |
56 | pthread_cond_signal (&fooCondUnderflow); | |
ddd69607 LR |
57 | pthread_mutex_unlock (&fooLock); |
58 | } | |
59 | ||
07d75182 LR |
60 | // No more data will ever be written, ensure no fini race |
61 | pthread_mutex_lock (&fooLock); | |
62 | pthread_cond_signal (&fooCondUnderflow); | |
63 | pthread_mutex_unlock (&fooLock); | |
64 | ||
ddd69607 LR |
65 | return 0; |
66 | } | |
67 | ||
68 | void* | |
69 | consume (void*) | |
70 | { | |
71 | for (int num = 0; num < iters; ) | |
72 | { | |
73 | pthread_mutex_lock (&fooLock); | |
b625fdb7 LR |
74 | while (foo.size () == 0) |
75 | pthread_cond_wait (&fooCondUnderflow, &fooLock); | |
ddd69607 LR |
76 | while (foo.size () > 0) |
77 | { | |
78 | string str = foo.back (); | |
79 | foo.pop_back (); | |
80 | num++; | |
81 | } | |
b625fdb7 | 82 | pthread_cond_signal (&fooCondOverflow); |
ddd69607 LR |
83 | pthread_mutex_unlock (&fooLock); |
84 | } | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
89 | int | |
90 | main (void) | |
91 | { | |
87bd0274 | 92 | #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 |
ddd69607 LR |
93 | pthread_setconcurrency (2); |
94 | #endif | |
95 | ||
96 | pthread_t prod; | |
8fc81078 | 97 | pthread_create (&prod, 0, produce, 0); |
ddd69607 | 98 | pthread_t cons; |
8fc81078 | 99 | pthread_create (&cons, 0, consume, 0); |
ddd69607 | 100 | |
8fc81078 PC |
101 | pthread_join (prod, 0); |
102 | pthread_join (cons, 0); | |
ddd69607 LR |
103 | |
104 | return 0; | |
105 | } |