From 5690b13c41b248f5957211ca69d4daf509a6b42f Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Thu, 1 Dec 2011 21:31:42 +0100 Subject: Thread affinity. --- src/thread.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/thread.cpp b/src/thread.cpp index 7de6b01..401b388 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -46,6 +46,26 @@ int Thread::start() { void* Thread::start_routine(void* p) { + // get the current affinity + cpu_set_t cs; + CPU_ZERO(&cs); + sched_getaffinity(0, sizeof(cs), &cs); + + // deduce the amount of CPUs + int count = 0; + for (int i = 0; i < 8; i++) + { + if (CPU_ISSET(i, &cs)) + count++; + } + + // restrict to a single CPU + CPU_ZERO(&cs); + size_t size = CPU_ALLOC_SIZE(1); + CPU_SET_S(((Thread*) p)->id % count, size, &cs); + sched_setaffinity(pthread_self(), size, &cs); + + // run ((Thread*) p)->run(); return NULL; -- cgit v1.2.3