* @brief Check, for a specific member function which parallelizes loops or sequences of tasks, that if a task that captures a shared pointer is submitted, the pointer is correctly shared between all the iterations of the task. * * @param which_func A string naming the function to check. */
| 2641 | * @param which_func A string naming the function to check. |
| 2642 | */ |
| 2643 | void check_shared_ptr(const std::string_view which_func) |
| 2644 | { |
| 2645 | BS::thread_pool pool; |
| 2646 | constexpr std::chrono::milliseconds sleep_time(10); |
| 2647 | const std::size_t num_tasks = pool.get_thread_count() * 10; |
| 2648 | std::atomic<bool> object_exists = false; |
| 2649 | std::atomic<std::size_t> uses_before_destruct = 0; |
| 2650 | std::atomic<std::size_t> uses_after_destruct = 0; |
| 2651 | logln("Checking ", which_func, "..."); |
| 2652 | { |
| 2653 | std::shared_ptr<detect_destruct> ptr = std::make_shared<detect_destruct>(&object_exists); |
| 2654 | auto task = [ptr, &object_exists, &uses_before_destruct, &uses_after_destruct, &sleep_time](auto&&...) |
| 2655 | { |
| 2656 | std::this_thread::sleep_for(sleep_time); |
| 2657 | if (object_exists) |
| 2658 | ++uses_before_destruct; |
| 2659 | else |
| 2660 | ++uses_after_destruct; |
| 2661 | }; |
| 2662 | if (which_func == "detach_blocks()") |
| 2663 | pool.detach_blocks(0, num_tasks, std::move(task), num_tasks); |
| 2664 | else if (which_func == "detach_loop()") |
| 2665 | pool.detach_loop(0, num_tasks, std::move(task)); |
| 2666 | else if (which_func == "detach_sequence()") |
| 2667 | pool.detach_sequence(0, num_tasks, std::move(task)); |
| 2668 | else if (which_func == "submit_blocks()") |
| 2669 | std::ignore = pool.submit_blocks(0, num_tasks, std::move(task), num_tasks); |
| 2670 | else if (which_func == "submit_loop()") |
| 2671 | std::ignore = pool.submit_loop(0, num_tasks, std::move(task)); |
| 2672 | else if (which_func == "submit_sequence()") |
| 2673 | std::ignore = pool.submit_sequence(0, num_tasks, std::move(task)); |
| 2674 | ptr.reset(); |
| 2675 | } |
| 2676 | pool.wait(); |
| 2677 | std::this_thread::sleep_for(sleep_time); |
| 2678 | logln("Uses before destruct:"); |
| 2679 | check(num_tasks, uses_before_destruct.load()); |
| 2680 | logln("Uses after destruct:"); |
| 2681 | check(0, uses_after_destruct.load()); |
| 2682 | } |
| 2683 | |
| 2684 | /** |
| 2685 | * @brief Check, for all member functions which parallelize loops or sequences of tasks, that if a task that captures a shared pointer is submitted, the pointer is correctly shared between all the iterations of the task. |
no test coverage detected