MCPcopy Create free account
hub / github.com/bshoshany/thread-pool / check_wait_blocks

Function check_wait_blocks

tests/BS_thread_pool_test.cpp:1450–1486  ·  view source on GitHub ↗

* @brief Check that wait() correctly blocks all external threads that call it. */

Source from the content-addressed store, hash-verified

1448 * @brief Check that wait() correctly blocks all external threads that call it.
1449 */
1450void check_wait_blocks()
1451{
1452 constexpr std::chrono::milliseconds sleep_time(100);
1453 constexpr std::size_t num_waiting_tasks = 4;
1454 BS::thread_pool pool;
1455 binary_semaphore sem(0);
1456 logln("Checking that wait() correctly blocks all external threads that call it...");
1457 pool.detach_task(
1458 [&sem]
1459 {
1460 logln("Task submitted to pool 1 and waiting to be released...");
1461 sem.acquire();
1462 logln("Task released.");
1463 });
1464 BS::thread_pool temp_pool(num_waiting_tasks);
1465 std::vector<std::atomic<bool>> flags(num_waiting_tasks);
1466 const auto waiting_task = [&flags, &pool](const std::size_t task_num)
1467 {
1468 logln("Task ", task_num, " submitted to pool 2 and waiting for pool 1's task to finish...");
1469 pool.wait();
1470 logln("Task ", task_num, " finished waiting.");
1471 flags[task_num] = true;
1472 };
1473 for (std::size_t i = 0; i < num_waiting_tasks; ++i)
1474 {
1475 temp_pool.detach_task(
1476 [&waiting_task, i]
1477 {
1478 waiting_task(i);
1479 });
1480 }
1481 std::this_thread::sleep_for(sleep_time);
1482 check(no_flags_set(flags));
1483 sem.release();
1484 temp_pool.wait();
1485 check(all_flags_set(flags));
1486}
1487
1488/**
1489 * @brief Check that wait_for() works.

Callers 1

mainFunction · 0.85

Calls 7

loglnFunction · 0.85
checkFunction · 0.85
no_flags_setFunction · 0.85
all_flags_setFunction · 0.85
acquireMethod · 0.80
waitMethod · 0.80
releaseMethod · 0.80

Tested by

no test coverage detected