You might want to use a thread and set its affinity.
Since you do not mention what OS you're on, if you're using Windows you'll need to use the SetThreadAffinityMask
function. If you're running on a POSIX based system (like Linux/Unix and even macOS to some extent), you can use the pthread_setaffinity_np
function, but you must take care with that function since the _np
means "non-portable" and might not work on your target system.
For both of these, they require a native thread handle, which in Windows is one created with CreateThread
or _beginthread
/_beginthreadex
(i.e. a HANDLE
), for POSIX systems it's threads created with pthread_create
(i.e. a pthread_t
).
If you're using the C11 <threads.h>
header and created a thrd_t
with thrd_create
, then you can pass that as the thread handle to the set-affinity functions, though you might have to cast it to the specific type (e.g. (HANDLE)tid
or (pthread_t)tid
).
If you switch to C++11 and use the std::thread
type, you will have to pass the value returned from the native_handle
call into the set-affinity functions.
This Q&A has some examples on how to use the pthread_setaffinity_np
, and this one has some info regarding SetThreadAffinityMask
.
As an aside, without seeing code, setting the affinity might not get you want you want regarding parallel performance, you'll need to run some tests on your code to verify that it actually does increase the performance of your code, versus writing tighter, more CPU-cache friendly code.
I hope that can help.