2

I use C++11 threading and need to set the affinity of a thread to a given processor core (windows' scheduler is not delegating to cores intelligently and I want control over what thread goes to what core).

For windows, there is

SetThreadAffinityMask(GetCurrentThread(), affinity);

What would a single cross-platform function look like that would work on windows, linux and OSX, with all the permutations of underlying threading libraries (windows threads, pthreads, etc.)?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
ibell
  • 1,070
  • 8
  • 29

1 Answers1

1

If part of C++, I suspect it would look something like the pair of member function:

std::error_code std::thread::hardware_affinity(const std::vector<bool>&);

and free function:

std::error_code std::this_thread::hardware_affinity(const std::vector<bool>&);

on the assumption that we're talking about roughly the same sort of resources that std::thread::hardware_concurrency() counts, and that std::thread implementations must already take care of "all the permutations of underlying threading libraries".

Building one yourself, or using a utility library (I can't name an existing one right now) it would probably look like:

thread_utils::hardware_affinity(std::thread::native_handle_type, const std::vector<bool>&)

but it would have to support all the permutations internally. That's not hugely hard (many applications have done it out of necessity anyway), but it's more work than a Stack Overflow answer can hold. ^_^

One annoying limitation with this approach is that although there is the member function std::thread::native_handle(), there's no std::this_thread::native_handle() method. It's basically pending a write-up and a good use case to be standardised.

Community
  • 1
  • 1
TBBle
  • 1,436
  • 10
  • 27
  • Side-note: [Executors](https://github.com/executors/issaquah_2016) possibly provides a better answer to this question, since it lets you deal with hardware cores as "execution resources", and you can use an "executor" to create "execution agents" which are bound to one or more of those resources. However, I don't understand the paper well enough to be sure that's right, and it's still being evolved. – TBBle May 10 '17 at 17:29
  • 1
    Can you point me to a library that has done the "many applications have done it out of necessity anyway" implementation? That's pretty much exactly what I was asking about in this question. I know how to get the native_handle, but that's not very useful if you don't know what to do with it :) – ibell May 12 '17 at 05:24
  • @ibell A quick code search for [`SetThreadAffinityMask` `pthread_setaffinity_np`](https://searchcode.com/?q=SetThreadAffinityMask+pthread_setaffinity_np) gave me: [Thread.cpp from the Dolphin emulator (GPL2)](https://searchcode.com/file/92960692/Source/Core/Common/Thread.cpp), and a later hit pointed me at the [LuaSys thread library](https://github.com/tnodir/luasys/tree/master/src/thread). – TBBle May 12 '17 at 16:44