]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/list/pthread1.cc
0c26fd0e62fa1f7cb4a108f64600498a08cf4410
1 // 2002-01-23 Loren J. Rittle <rittle@labs.mot.com> <ljrittle@acm.org>
3 // Copyright (C) 2002-2023 Free Software Foundation, Inc.
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 3, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // { dg-options "-pthread" }
22 // { dg-require-effective-target pthread }
24 // This multi-threading C++/STL/POSIX code adheres to rules outlined here:
25 // http://www.sgi.com/tech/stl/thread_safety.html
27 // It is believed to exercise the allocation code in a manner that
28 // should reveal memory leaks (and, under rare cases, race conditions,
29 // if the STL threading support is fubar'd).
35 const int thread_cycles
= 10;
36 const int thread_pairs
= 10;
37 const unsigned max_size
= 100;
38 const int iters
= 10000;
42 typedef std::list
<int> list_type
;
47 pthread_mutex_init (&fooLock
, 0);
48 pthread_cond_init (&fooCond1
, 0);
49 pthread_cond_init (&fooCond2
, 0);
53 pthread_mutex_destroy (&fooLock
);
54 pthread_cond_destroy (&fooCond1
);
55 pthread_cond_destroy (&fooCond2
);
59 pthread_mutex_t fooLock
;
60 pthread_cond_t fooCond1
;
61 pthread_cond_t fooCond2
;
67 task_queue
& tq
= *(static_cast<task_queue
*> (t
));
71 pthread_mutex_lock (&tq
.fooLock
);
72 while (tq
.foo
.size () >= max_size
)
73 pthread_cond_wait (&tq
.fooCond1
, &tq
.fooLock
);
74 tq
.foo
.push_back (num
++);
75 pthread_cond_signal (&tq
.fooCond2
);
76 pthread_mutex_unlock (&tq
.fooLock
);
84 task_queue
& tq
= *(static_cast<task_queue
*> (t
));
88 pthread_mutex_lock (&tq
.fooLock
);
89 while (tq
.foo
.size () == 0)
90 pthread_cond_wait (&tq
.fooCond2
, &tq
.fooLock
);
91 if (tq
.foo
.front () != num
++)
94 pthread_cond_signal (&tq
.fooCond1
);
95 pthread_mutex_unlock (&tq
.fooLock
);
103 pthread_t prod
[thread_pairs
];
104 pthread_t cons
[thread_pairs
];
106 task_queue
* tq
[thread_pairs
];
108 #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500
109 pthread_setconcurrency (thread_pairs
* 2);
112 for (int j
= 0; j
< thread_cycles
; j
++)
114 for (int i
= 0; i
< thread_pairs
; i
++)
116 tq
[i
] = new task_queue
;
117 pthread_create (&prod
[i
], 0, produce
, static_cast<void*> (tq
[i
]));
118 pthread_create (&cons
[i
], 0, consume
, static_cast<void*> (tq
[i
]));
121 for (int i
= 0; i
< thread_pairs
; i
++)
123 pthread_join (prod
[i
], 0);
124 pthread_join (cons
[i
], 0);