1142215Sglebius// 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org> 2142215Sglebius// Adapted from http://gcc.gnu.org/ml/gcc-bugs/2002-01/msg00679.html 3142215Sglebius// which was adapted from pthread1.cc by Mike Lu <MLu@dynamicsoft.com> 4142215Sglebius// 5142215Sglebius// Copyright (C) 2002-2015 Free Software Foundation, Inc. 6142215Sglebius// 7142215Sglebius// This file is part of the GNU ISO C++ Library. This library is free 8142215Sglebius// software; you can redistribute it and/or modify it under the 9142215Sglebius// terms of the GNU General Public License as published by the 10142215Sglebius// Free Software Foundation; either version 3, or (at your option) 11142215Sglebius// any later version. 12142215Sglebius// 13142215Sglebius// This library is distributed in the hope that it will be useful, 14142215Sglebius// but WITHOUT ANY WARRANTY; without even the implied warranty of 15142215Sglebius// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16142215Sglebius// GNU General Public License for more details. 17142215Sglebius// 18142215Sglebius// You should have received a copy of the GNU General Public License along 19142215Sglebius// with this library; see the file COPYING3. If not see 20142215Sglebius// <http://www.gnu.org/licenses/>. 21142215Sglebius 22142215Sglebius// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* } } 23142215Sglebius// { dg-options "-pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* } } 24142215Sglebius// { dg-options "-pthreads" { target *-*-solaris* } } 25142215Sglebius 26142215Sglebius#include <string> 27142215Sglebius#include <list> 28142215Sglebius#include <pthread.h> 29142215Sglebius 30142215Sglebiususing namespace std; 31142215Sglebius 32142215Sglebiusstatic list<string> foo; 33142215Sglebiusstatic pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER; 34142215Sglebiusstatic pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER; 35142215Sglebiusstatic pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER; 36142215Sglebiusstatic unsigned max_size = 10; 37142215Sglebius#if defined(__CYGWIN__) 38142215Sglebiusstatic int iters = 10000; 39142215Sglebius#else 40142215Sglebiusstatic int iters = 300000; 41142215Sglebius#endif 42142215Sglebius 43142215Sglebiusvoid* 44164033Srwatsonproduce (void*) 45142215Sglebius{ 46142215Sglebius for (int num = 0; num < iters; ) 47142215Sglebius { 48142215Sglebius string str ("test string"); 49142215Sglebius 50142215Sglebius pthread_mutex_lock (&fooLock); 51142215Sglebius while (foo.size () >= max_size) 52142215Sglebius pthread_cond_wait (&fooCondOverflow, &fooLock); 53142215Sglebius foo.push_back (str); 54142215Sglebius num++; 55142215Sglebius if (foo.size () >= (max_size / 2)) 56142215Sglebius pthread_cond_signal (&fooCondUnderflow); 57142215Sglebius pthread_mutex_unlock (&fooLock); 58142215Sglebius } 59142215Sglebius 60142215Sglebius // No more data will ever be written, ensure no fini race 61142215Sglebius pthread_mutex_lock (&fooLock); 62142215Sglebius pthread_cond_signal (&fooCondUnderflow); 63152410Sru pthread_mutex_unlock (&fooLock); 64142215Sglebius 65142215Sglebius return 0; 66142215Sglebius} 67142215Sglebius 68142215Sglebiusvoid* 69142215Sglebiusconsume (void*) 70142215Sglebius{ 71142215Sglebius for (int num = 0; num < iters; ) 72142215Sglebius { 73142215Sglebius pthread_mutex_lock (&fooLock); 74142215Sglebius while (foo.size () == 0) 75142215Sglebius pthread_cond_wait (&fooCondUnderflow, &fooLock); 76142215Sglebius while (foo.size () > 0) 77142215Sglebius { 78142215Sglebius string str = foo.back (); 79142215Sglebius foo.pop_back (); 80142215Sglebius num++; 81148387Sume } 82142215Sglebius pthread_cond_signal (&fooCondOverflow); 83142215Sglebius pthread_mutex_unlock (&fooLock); 84142215Sglebius } 85142215Sglebius 86142215Sglebius return 0; 87142215Sglebius} 88142215Sglebius 89142215Sglebiusint 90142215Sglebiusmain (void) 91142215Sglebius{ 92142215Sglebius#if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 93147256Sbrooks pthread_setconcurrency (2); 94142901Sglebius#endif 95142215Sglebius 96142215Sglebius pthread_t prod; 97142215Sglebius pthread_create (&prod, 0, produce, 0); 98142215Sglebius pthread_t cons; 99142215Sglebius pthread_create (&cons, 0, consume, 0); 100142215Sglebius 101142215Sglebius pthread_join (prod, 0); 102142215Sglebius pthread_join (cons, 0); 103142215Sglebius 104142215Sglebius return 0; 105142215Sglebius} 106142215Sglebius