xref: /llvm-project/libcxx/test/libcxx/debug/containers.multithread.pass.cpp (revision e61764c32ff77ae85509e56234a13a47b2d36cec)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++11, c++14
10 // UNSUPPORTED: no-threads
11 
12 // UNSUPPORTED: !libcpp-has-legacy-debug-mode, c++03
13 
14 // test multithreaded container debugging
15 
16 #include <cassert>
17 #include <cstddef>
18 #include <deque>
19 #include <list>
20 #include <thread>
21 #include <vector>
22 
23 template <typename Container>
makeContainer(int size)24 Container makeContainer(int size) {
25   Container c;
26   typedef typename Container::value_type ValueType;
27   for (int i = 0; i < size; ++i)
28     c.insert(c.end(), ValueType(i));
29   assert(c.size() == static_cast<std::size_t>(size));
30   return c;
31 }
32 
33 template <typename Container>
ThreadUseIter()34 void ThreadUseIter() {
35   const std::size_t maxRounds = 7;
36   struct TestRunner{
37     void operator()() {
38       for (std::size_t count = 0; count < maxRounds; count++) {
39         const std::size_t containerCount = 11;
40         std::vector<Container> containers;
41         std::vector<typename Container::iterator> iterators;
42         for (std::size_t containerIndex = 0; containerIndex < containerCount; containerIndex++) {
43           containers.push_back(makeContainer<Container>(3));
44           Container& c = containers.back();
45           iterators.push_back(c.begin());
46           iterators.push_back(c.end());
47         }
48       }
49     }
50   };
51 
52   TestRunner r;
53   const std::size_t threadCount = 4;
54   std::vector<std::thread> threads;
55   for (std::size_t count = 0; count < threadCount; count++)
56     threads.emplace_back(r);
57   r();
58   for (std::size_t count = 0; count < threadCount; count++)
59     threads[count].join();
60 }
61 
main(int,char **)62 int main(int, char**) {
63   ThreadUseIter<std::vector<int> >();
64   return 0;
65 }
66