diff options
author | Tim Besard <tim.besard@gmail.com> | 2011-12-01 21:31:42 +0100 |
---|---|---|
committer | Tim Besard <tim.besard@gmail.com> | 2011-12-01 21:31:42 +0100 |
commit | 5690b13c41b248f5957211ca69d4daf509a6b42f (patch) | |
tree | d5fc3d1c338f276601551f7afc65c5d066d36658 | |
parent | 30d478c625791fddb5cc0400552503d04e03721b (diff) |
Thread affinity.
-rw-r--r-- | src/thread.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
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; |