]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/thread/pthread1.cc
testsuite_hooks.h (func_callback): Declare copy constructor and assignment operator...
[gcc.git] / libstdc++-v3 / testsuite / thread / pthread1.cc
1 // 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 //
3 // Copyright (C) 2002, 2003, 2004 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 2, 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 COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* } }
22 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* } }
23 // { dg-options "-pthreads" { target *-*-solaris* } }
24
25 // This multi-threading C++/STL/POSIX code adheres to rules outlined here:
26 // http://www.sgi.com/tech/stl/thread_safety.html
27 //
28 // It is believed to exercise the allocation code in a manner that
29 // should reveal memory leaks (and, under rare cases, race conditions,
30 // if the STL threading support is fubar'd).
31
32 #include <list>
33
34 // Do not include <pthread.h> explicitly; if threads are properly
35 // configured for the port, then it is picked up free from STL headers.
36
37 #if __GTHREADS
38 using namespace std;
39
40 const int thread_cycles = 10;
41 const int thread_pairs = 10;
42 const unsigned max_size = 100;
43 const int iters = 10000;
44
45 class task_queue
46 {
47 public:
48 task_queue ()
49 {
50 pthread_mutex_init (&fooLock, NULL);
51 pthread_cond_init (&fooCond1, NULL);
52 pthread_cond_init (&fooCond2, NULL);
53 }
54 ~task_queue ()
55 {
56 pthread_mutex_destroy (&fooLock);
57 pthread_cond_destroy (&fooCond1);
58 pthread_cond_destroy (&fooCond2);
59 }
60 list<int> foo;
61 pthread_mutex_t fooLock;
62 pthread_cond_t fooCond1;
63 pthread_cond_t fooCond2;
64 };
65
66 void*
67 produce (void* t)
68 {
69 task_queue& tq = *(static_cast<task_queue*> (t));
70 int num = 0;
71 while (num < iters)
72 {
73 pthread_mutex_lock (&tq.fooLock);
74 while (tq.foo.size () >= max_size)
75 pthread_cond_wait (&tq.fooCond1, &tq.fooLock);
76 tq.foo.push_back (num++);
77 pthread_cond_signal (&tq.fooCond2);
78 pthread_mutex_unlock (&tq.fooLock);
79 }
80 return 0;
81 }
82
83 void*
84 consume (void* t)
85 {
86 task_queue& tq = *(static_cast<task_queue*> (t));
87 int num = 0;
88 while (num < iters)
89 {
90 pthread_mutex_lock (&tq.fooLock);
91 while (tq.foo.size () == 0)
92 pthread_cond_wait (&tq.fooCond2, &tq.fooLock);
93 if (tq.foo.front () != num++)
94 abort ();
95 tq.foo.pop_front ();
96 pthread_cond_signal (&tq.fooCond1);
97 pthread_mutex_unlock (&tq.fooLock);
98 }
99 return 0;
100 }
101
102 #if !__GXX_WEAK__ && _MT_ALLOCATOR_H
103 // Explicitly instantiate for systems with no COMDAT or weak support.
104 template class __gnu_cxx::__mt_alloc<std::_List_node<int> >;
105 #endif
106
107 int
108 main ()
109 {
110 pthread_t prod[thread_pairs];
111 pthread_t cons[thread_pairs];
112
113 task_queue* tq[thread_pairs];
114
115 #if defined(__sun) && defined(__svr4__)
116 pthread_setconcurrency (thread_pairs * 2);
117 #endif
118
119 for (int j = 0; j < thread_cycles; j++)
120 {
121 for (int i = 0; i < thread_pairs; i++)
122 {
123 tq[i] = new task_queue;
124 pthread_create (&prod[i], NULL, produce, static_cast<void*> (tq[i]));
125 pthread_create (&cons[i], NULL, consume, static_cast<void*> (tq[i]));
126 }
127
128 for (int i = 0; i < thread_pairs; i++)
129 {
130 pthread_join (prod[i], NULL);
131 pthread_join (cons[i], NULL);
132 delete tq[i];
133 }
134 }
135
136 return 0;
137 }
138 #else
139 int main (void) {}
140 #endif
This page took 0.040319 seconds and 5 git commands to generate.