2004-10-06 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / thread / pthread4.cc
bloba9ec9b84e3878b6a5b2524b42f61b813d691115a
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 //
5 // Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
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
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
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.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
23 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* } }
24 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
27 #include <string>
28 #include <list>
30 // Do not include <pthread.h> explicitly; if threads are properly
31 // configured for the port, then it is picked up free from STL headers.
33 using namespace std;
35 static list<string> foo;
36 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
37 static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER;
38 static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER;
39 static unsigned max_size = 10;
40 #if defined(__CYGWIN__)
41 static int iters = 10000;
42 #else
43 static int iters = 300000;
44 #endif
46 void*
47 produce (void*)
49 for (int num = 0; num < iters; )
51 string str ("test string");
53 pthread_mutex_lock (&fooLock);
54 while (foo.size () >= max_size)
55 pthread_cond_wait (&fooCondOverflow, &fooLock);
56 foo.push_back (str);
57 num++;
58 if (foo.size () >= (max_size / 2))
59 pthread_cond_signal (&fooCondUnderflow);
60 pthread_mutex_unlock (&fooLock);
63 // No more data will ever be written, ensure no fini race
64 pthread_mutex_lock (&fooLock);
65 pthread_cond_signal (&fooCondUnderflow);
66 pthread_mutex_unlock (&fooLock);
68 return 0;
71 void*
72 consume (void*)
74 for (int num = 0; num < iters; )
76 pthread_mutex_lock (&fooLock);
77 while (foo.size () == 0)
78 pthread_cond_wait (&fooCondUnderflow, &fooLock);
79 while (foo.size () > 0)
81 string str = foo.back ();
82 foo.pop_back ();
83 num++;
85 pthread_cond_signal (&fooCondOverflow);
86 pthread_mutex_unlock (&fooLock);
89 return 0;
92 #if !__GXX_WEAK__ && _MT_ALLOCATOR_H
93 // Explicitly instantiate for systems with no COMDAT or weak support.
94 template class __gnu_cxx::__mt_alloc<std::string>;
95 template class __gnu_cxx::__mt_alloc<std::_List_node<std::string> >;
96 #endif
98 int
99 main (void)
101 #if defined(__sun) && defined(__svr4__)
102 pthread_setconcurrency (2);
103 #endif
105 pthread_t prod;
106 pthread_create (&prod, NULL, produce, NULL);
107 pthread_t cons;
108 pthread_create (&cons, NULL, consume, NULL);
110 pthread_join (prod, NULL);
111 pthread_join (cons, NULL);
113 return 0;