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

Function check_task_monitoring

tests/BS_thread_pool_test.cpp:2049–2085  ·  view source on GitHub ↗

* @brief Check that task monitoring works. */

Source from the content-addressed store, hash-verified

2047 * @brief Check that task monitoring works.
2048 */
2049void check_task_monitoring()
2050{
2051 constexpr std::chrono::milliseconds sleep_time(300);
2052 const std::size_t num_threads = std::min<std::size_t>(std::thread::hardware_concurrency(), 4);
2053 logln("Creating pool with ", num_threads, " threads.");
2054 BS::thread_pool pool(num_threads);
2055 logln("Submitting ", num_threads * 3, " tasks.");
2056 counting_semaphore sem(0);
2057 for (std::size_t i = 0; i < num_threads * 3; ++i)
2058 {
2059 pool.detach_task(
2060 [i, &sem]
2061 {
2062 sem.acquire();
2063 logln("Task ", i, " released.");
2064 });
2065 }
2066 std::this_thread::sleep_for(sleep_time);
2067 logln("After submission, should have: ", num_threads * 3, " tasks total, ", num_threads, " tasks running, ", num_threads * 2, " tasks queued...");
2068 log("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued ");
2069 check(pool.get_tasks_total() == num_threads * 3 && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == num_threads * 2);
2070 sem.release(static_cast<std::ptrdiff_t>(num_threads));
2071 std::this_thread::sleep_for(sleep_time);
2072 logln("After releasing ", num_threads, " tasks, should have: ", num_threads * 2, " tasks total, ", num_threads, " tasks running, ", num_threads, " tasks queued...");
2073 log("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued ");
2074 check(pool.get_tasks_total() == num_threads * 2 && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == num_threads);
2075 sem.release(static_cast<std::ptrdiff_t>(num_threads));
2076 std::this_thread::sleep_for(sleep_time);
2077 logln("After releasing ", num_threads, " more tasks, should have: ", num_threads, " tasks total, ", num_threads, " tasks running, ", 0, " tasks queued...");
2078 log("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued ");
2079 check(pool.get_tasks_total() == num_threads && pool.get_tasks_running() == num_threads && pool.get_tasks_queued() == 0);
2080 sem.release(static_cast<std::ptrdiff_t>(num_threads));
2081 std::this_thread::sleep_for(sleep_time);
2082 logln("After releasing the final ", num_threads, " tasks, should have: ", 0, " tasks total, ", 0, " tasks running, ", 0, " tasks queued...");
2083 log("Result: ", pool.get_tasks_total(), " tasks total, ", pool.get_tasks_running(), " tasks running, ", pool.get_tasks_queued(), " tasks queued ");
2084 check(pool.get_tasks_total() == 0 && pool.get_tasks_running() == 0 && pool.get_tasks_queued() == 0);
2085}
2086
2087/**
2088 * @brief Check that pausing works.

Callers 1

mainFunction · 0.85

Calls 5

loglnFunction · 0.85
logFunction · 0.85
checkFunction · 0.85
acquireMethod · 0.80
releaseMethod · 0.80

Tested by

no test coverage detected