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

Method set_os_thread_affinity

include/BS_thread_pool.hpp:1048–1067  ·  view source on GitHub ↗

* @brief Set the processor affinity of the current thread using the current platform's native API. This should work on Windows and Linux, but is not possible on macOS and Android as the native API does not allow it. Note that the thread affinity must be a subset of the process affinity (as obtained using `BS::get_os_process_affinity()`) for the containing process of a thread. * * @param

Source from the content-addressed store, hash-verified

1046 * @return `true` if the affinity was set successfully, `false` otherwise. On macOS and Android, this function always returns `false`.
1047 */
1048 static bool set_os_thread_affinity([[maybe_unused]] const std::vector<bool>& affinity)
1049 {
1050 #if defined(_WIN32)
1051 DWORD_PTR thread_mask = 0;
1052 for (std::size_t i = 0; i < std::min<std::size_t>(affinity.size(), sizeof(DWORD_PTR) * 8); ++i)
1053 thread_mask |= (affinity[i] ? (1ULL << i) : 0ULL);
1054 return SetThreadAffinityMask(GetCurrentThread(), thread_mask) != 0;
1055 #elif defined(__linux__) && !defined(__ANDROID__)
1056 cpu_set_t cpu_set;
1057 CPU_ZERO(&cpu_set);
1058 for (std::size_t i = 0; i < std::min<std::size_t>(affinity.size(), CPU_SETSIZE); ++i)
1059 {
1060 if (affinity[i])
1061 CPU_SET(i, &cpu_set);
1062 }
1063 return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set) == 0;
1064 #else
1065 return false;
1066 #endif
1067 }
1068
1069 /**
1070 * @brief Get the name of the current thread using the current platform's native API. This should work on Windows, Linux, and macOS.

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected