MCPcopy Create free account
hub / github.com/boostorg/leaf / test_multithreaded

Function test_multithreaded

test/so_dll_test.cpp:144–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

142}
143
144void test_multithreaded()
145{
146 constexpr int N = 100;
147 {
148 std::mutex mtx;
149 std::condition_variable cv;
150 bool ready = false;
151 auto test_function = [&]
152 {
153 std::unique_lock<std::mutex> lock(mtx);
154 cv.wait(lock, [&]{ return ready; });
155 lock.unlock();
156 int result1 = test_result(hidden_result1, false);
157 int result2 = test_result(hidden_result2, false);
158 return result1 + result2;
159 };
160 std::vector<std::future<int>> futures;
161 for (int i = 0; i < N; ++i)
162 futures.push_back(std::async(std::launch::async, test_function));
163 {
164 std::lock_guard<std::mutex> lock(mtx);
165 ready = true;
166 }
167 cv.notify_all();
168 for (auto start = std::chrono::steady_clock::now(); std::chrono::steady_clock::now() - start < std::chrono::seconds(1); )
169 if ((std::rand() % 2) == 0 || futures.empty())
170 futures.push_back(std::async(std::launch::async, test_function));
171 else
172 {
173 BOOST_TEST_EQ(futures.back().get(), 0);
174 futures.pop_back();
175 }
176 for (auto & f : futures)
177 BOOST_TEST_EQ(f.get(), 0);
178 }
179
180#ifndef BOOST_LEAF_NO_EXCEPTIONS
181 {
182 std::mutex mtx;
183 std::condition_variable cv;
184 bool ready = false;
185 auto test_function = [&]
186 {
187 std::unique_lock<std::mutex> lock(mtx);
188 cv.wait(lock, [&]{ return ready; });
189 lock.unlock();
190 int result1 = test_exception(hidden_throw1, false);
191 int result2 = test_catch(hidden_throw1);
192 int result3 = test_exception(hidden_throw2, false);
193 int result4 = test_catch(hidden_throw2);
194 return result1 + result2 + result3 + result4;
195 };
196 std::vector<std::future<int>> futures;
197 for (int i = 0; i < N; ++i)
198 futures.push_back(std::async(std::launch::async, test_function));
199 {
200 std::lock_guard<std::mutex> lock(mtx);
201 ready = true;

Callers 1

mainFunction · 0.85

Calls 4

test_exceptionFunction · 0.85
test_catchFunction · 0.85
test_resultFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected