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

Function determine_thread_count

include/BS_thread_pool.hpp:2125–2140  ·  view source on GitHub ↗

* @brief Determine how many threads the pool should have, based on the parameter passed to the constructor or reset(). * * @param num_threads The parameter passed to the constructor or `reset()`. If the parameter is a positive number, then the pool will be created with this number of threads. If the parameter is zero, or a parameter was not supplied (in which case it will have the defaul

Source from the content-addressed store, hash-verified

2123 * @param num_threads The parameter passed to the constructor or `reset()`. If the parameter is a positive number, then the pool will be created with this number of threads. If the parameter is zero, or a parameter was not supplied (in which case it will have the default value of 0), then the pool will be created with the total number of hardware threads available, as obtained from `thread_t::hardware_concurrency()`. If the latter returns zero for some reason, then the pool will be created with just one thread. If the native extensions are enabled, the pool will instead use the number of threads available to the process, as obtained from `BS::get_os_process_affinity()`, which can be less than the number of hardware threads.
2124 */
2125 [[nodiscard]] static std::size_t determine_thread_count(const std::size_t num_threads) noexcept(!thread_pool_native_extensions)
2126 {
2127 if (num_threads > 0)
2128 return num_threads;
2129#ifdef BS_THREAD_POOL_NATIVE_EXTENSIONS
2130 const std::optional<std::vector<bool>> affinity = BS::get_os_process_affinity();
2131 if (affinity.has_value())
2132 {
2133 const std::size_t affinity_thread_count = static_cast<std::size_t>(std::count(affinity->begin(), affinity->end(), true));
2134 return (affinity_thread_count > 0) ? affinity_thread_count : 1;
2135 }
2136#endif
2137 if (thread_t::hardware_concurrency() > 0)
2138 return thread_t::hardware_concurrency();
2139 return 1;
2140 }
2141
2142 /**
2143 * @brief A helper function for `detach_blocks()` and `submit_blocks()`.

Callers 1

create_threadsFunction · 0.85

Calls 2

get_os_process_affinityFunction · 0.85
endMethod · 0.80

Tested by

no test coverage detected