#include #include #include #include constexpr unsigned N = 3; void func(int c) { cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(c, &cpuset); int rc = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset); assert(!rc); for (auto i = 0; i < 1000000; i++) { try { throw 42; } catch (...) { } } } int main() { std::vector threads; threads.reserve(N); for (unsigned i = 0; i < N; i++) threads.emplace_back(std::thread( [i] { func(i); } )); func(N); for (unsigned i = 0; i < N; i++) threads[i].join(); return 0; }