| 1 | // 2002-01-23 Loren J. Rittle <[email protected]> <[email protected]> |
| 2 | // |
| 3 | // Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | // |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // { dg-do run } |
| 21 | // { dg-options "-pthread" } |
| 22 | // { dg-require-effective-target pthread } |
| 23 | |
| 24 | // This multi-threading C++/STL/POSIX code adheres to rules outlined here: |
| 25 | // http://www.sgi.com/tech/stl/thread_safety.html |
| 26 | // |
| 27 | // It is believed to exercise the allocation code in a manner that |
| 28 | // should reveal memory leaks (and, under rare cases, race conditions, |
| 29 | // if the STL threading support is fubar'd). |
| 30 | |
| 31 | #include <list> |
| 32 | #include <cstdlib> |
| 33 | #include <pthread.h> |
| 34 | |
| 35 | const int thread_cycles = 10; |
| 36 | const int thread_pairs = 10; |
| 37 | const unsigned max_size = 100; |
| 38 | const int iters = 10000; |
| 39 | |
| 40 | class task_queue |
| 41 | { |
| 42 | typedef std::list<int> list_type; |
| 43 | |
| 44 | public: |
| 45 | task_queue () |
| 46 | { |
| 47 | pthread_mutex_init (&fooLock, 0); |
| 48 | pthread_cond_init (&fooCond1, 0); |
| 49 | pthread_cond_init (&fooCond2, 0); |
| 50 | } |
| 51 | ~task_queue () |
| 52 | { |
| 53 | pthread_mutex_destroy (&fooLock); |
| 54 | pthread_cond_destroy (&fooCond1); |
| 55 | pthread_cond_destroy (&fooCond2); |
| 56 | } |
| 57 | |
| 58 | list_type foo; |
| 59 | pthread_mutex_t fooLock; |
| 60 | pthread_cond_t fooCond1; |
| 61 | pthread_cond_t fooCond2; |
| 62 | }; |
| 63 | |
| 64 | void* |
| 65 | produce(void* t) |
| 66 | { |
| 67 | task_queue& tq = *(static_cast<task_queue*> (t)); |
| 68 | int num = 0; |
| 69 | while (num < iters) |
| 70 | { |
| 71 | pthread_mutex_lock (&tq.fooLock); |
| 72 | while (tq.foo.size () >= max_size) |
| 73 | pthread_cond_wait (&tq.fooCond1, &tq.fooLock); |
| 74 | tq.foo.push_back (num++); |
| 75 | pthread_cond_signal (&tq.fooCond2); |
| 76 | pthread_mutex_unlock (&tq.fooLock); |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void* |
| 82 | consume(void* t) |
| 83 | { |
| 84 | task_queue& tq = *(static_cast<task_queue*> (t)); |
| 85 | int num = 0; |
| 86 | while (num < iters) |
| 87 | { |
| 88 | pthread_mutex_lock (&tq.fooLock); |
| 89 | while (tq.foo.size () == 0) |
| 90 | pthread_cond_wait (&tq.fooCond2, &tq.fooLock); |
| 91 | if (tq.foo.front () != num++) |
| 92 | abort (); |
| 93 | tq.foo.pop_front (); |
| 94 | pthread_cond_signal (&tq.fooCond1); |
| 95 | pthread_mutex_unlock (&tq.fooLock); |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | main() |
| 102 | { |
| 103 | pthread_t prod[thread_pairs]; |
| 104 | pthread_t cons[thread_pairs]; |
| 105 | |
| 106 | task_queue* tq[thread_pairs]; |
| 107 | |
| 108 | #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 |
| 109 | pthread_setconcurrency (thread_pairs * 2); |
| 110 | #endif |
| 111 | |
| 112 | for (int j = 0; j < thread_cycles; j++) |
| 113 | { |
| 114 | for (int i = 0; i < thread_pairs; i++) |
| 115 | { |
| 116 | tq[i] = new task_queue; |
| 117 | pthread_create (&prod[i], 0, produce, static_cast<void*> (tq[i])); |
| 118 | pthread_create (&cons[i], 0, consume, static_cast<void*> (tq[i])); |
| 119 | } |
| 120 | |
| 121 | for (int i = 0; i < thread_pairs; i++) |
| 122 | { |
| 123 | pthread_join (prod[i], 0); |
| 124 | pthread_join (cons[i], 0); |
| 125 | delete tq[i]; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |