* @brief Check that thread initialization functions and get_index() work. */
| 2407 | * @brief Check that thread initialization functions and get_index() work. |
| 2408 | */ |
| 2409 | void check_init() |
| 2410 | { |
| 2411 | logln("Comparing thread indices reported by get_index() using an initialization function passed to reset():"); |
| 2412 | std::vector<std::atomic<std::size_t>> thread_indices(std::thread::hardware_concurrency()); |
| 2413 | std::atomic<bool> correct = true; |
| 2414 | BS::thread_pool pool; |
| 2415 | pool.reset( |
| 2416 | [&thread_indices, &correct](std::size_t idx) |
| 2417 | { |
| 2418 | const std::optional<std::size_t> reported_idx = BS::this_thread::get_index(); |
| 2419 | if (reported_idx.has_value()) |
| 2420 | thread_indices[idx] = reported_idx.value(); |
| 2421 | else |
| 2422 | correct = false; |
| 2423 | }); |
| 2424 | pool.wait(); |
| 2425 | logln("Checking that all reported indices have values..."); |
| 2426 | check(correct); |
| 2427 | correct = true; |
| 2428 | for (std::size_t i = 0; i < thread_indices.size(); ++i) |
| 2429 | { |
| 2430 | if (thread_indices[i] != i) |
| 2431 | { |
| 2432 | correct = false; |
| 2433 | break; |
| 2434 | } |
| 2435 | } |
| 2436 | logln("Checking that all reported indices are correct..."); |
| 2437 | check(correct); |
| 2438 | |
| 2439 | logln("Verifying that the index of the main thread has no value..."); |
| 2440 | const std::optional<std::size_t> main_idx = BS::this_thread::get_index(); |
| 2441 | check(!main_idx.has_value()); |
| 2442 | |
| 2443 | logln("Verifying that the index of an independent thread has no value..."); |
| 2444 | std::thread test_thread( |
| 2445 | [] |
| 2446 | { |
| 2447 | const std::optional<std::size_t> ind_idx = BS::this_thread::get_index(); |
| 2448 | check(!ind_idx.has_value()); |
| 2449 | }); |
| 2450 | test_thread.join(); |
| 2451 | } |
| 2452 | |
| 2453 | /** |
| 2454 | * @brief Check that thread cleanup functions work. |