2010-06-03 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / libstdc++-v3 / testsuite / 23_containers / list / pthread1.cc
blob7a1de4d660773751b3de2b5624d374e20c805cb4
1 // 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
2 //
3 // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
22 // { dg-options "-pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
23 // { dg-options "-pthreads" { target *-*-solaris* } }
25 // This multi-threading C++/STL/POSIX code adheres to rules outlined here:
26 // http://www.sgi.com/tech/stl/thread_safety.html
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).
32 #include <list>
33 #include <cstdlib>
34 #include <cstddef>
35 #include <pthread.h>
37 const int thread_cycles = 10;
38 const int thread_pairs = 10;
39 const unsigned max_size = 100;
40 const int iters = 10000;
42 class task_queue
44 typedef std::list<int> list_type;
46 public:
47 task_queue ()
49 pthread_mutex_init (&fooLock, NULL);
50 pthread_cond_init (&fooCond1, NULL);
51 pthread_cond_init (&fooCond2, NULL);
53 ~task_queue ()
55 pthread_mutex_destroy (&fooLock);
56 pthread_cond_destroy (&fooCond1);
57 pthread_cond_destroy (&fooCond2);
60 list_type foo;
61 pthread_mutex_t fooLock;
62 pthread_cond_t fooCond1;
63 pthread_cond_t fooCond2;
66 void*
67 produce(void* t)
69 task_queue& tq = *(static_cast<task_queue*> (t));
70 int num = 0;
71 while (num < iters)
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);
80 return 0;
83 void*
84 consume(void* t)
86 task_queue& tq = *(static_cast<task_queue*> (t));
87 int num = 0;
88 while (num < iters)
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);
99 return 0;
103 main()
105 pthread_t prod[thread_pairs];
106 pthread_t cons[thread_pairs];
108 task_queue* tq[thread_pairs];
110 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
111 pthread_setconcurrency (thread_pairs * 2);
112 #endif
114 for (int j = 0; j < thread_cycles; j++)
116 for (int i = 0; i < thread_pairs; i++)
118 tq[i] = new task_queue;
119 pthread_create (&prod[i], NULL, produce, static_cast<void*> (tq[i]));
120 pthread_create (&cons[i], NULL, consume, static_cast<void*> (tq[i]));
123 for (int i = 0; i < thread_pairs; i++)
125 pthread_join (prod[i], NULL);
126 pthread_join (cons[i], NULL);
127 delete tq[i];
131 return 0;