]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/pthread4.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / pthread4.cc
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, 2005, 2006, 2007, 2009
6 // Free Software Foundation, Inc.
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
11 // Free Software Foundation; either version 3, or (at your option)
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
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
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* } }
25 // { dg-options "-pthreads" { target *-*-solaris* } }
26
27 #include <string>
28 #include <list>
29 #include <pthread.h>
30
31 using namespace std;
32
33 static list<string> foo;
34 static pthread_mutex_t fooLock = PTHREAD_MUTEX_INITIALIZER;
35 static pthread_cond_t fooCondOverflow = PTHREAD_COND_INITIALIZER;
36 static pthread_cond_t fooCondUnderflow = PTHREAD_COND_INITIALIZER;
37 static unsigned max_size = 10;
38 #if defined(__CYGWIN__)
39 static int iters = 10000;
40 #else
41 static int iters = 300000;
42 #endif
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);
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);
58 pthread_mutex_unlock (&fooLock);
59 }
60
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
66 return 0;
67 }
68
69 void*
70 consume (void*)
71 {
72 for (int num = 0; num < iters; )
73 {
74 pthread_mutex_lock (&fooLock);
75 while (foo.size () == 0)
76 pthread_cond_wait (&fooCondUnderflow, &fooLock);
77 while (foo.size () > 0)
78 {
79 string str = foo.back ();
80 foo.pop_back ();
81 num++;
82 }
83 pthread_cond_signal (&fooCondOverflow);
84 pthread_mutex_unlock (&fooLock);
85 }
86
87 return 0;
88 }
89
90 int
91 main (void)
92 {
93 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
94 pthread_setconcurrency (2);
95 #endif
96
97 pthread_t prod;
98 pthread_create (&prod, NULL, produce, NULL);
99 pthread_t cons;
100 pthread_create (&cons, NULL, consume, NULL);
101
102 pthread_join (prod, NULL);
103 pthread_join (cons, NULL);
104
105 return 0;
106 }