Working with threads in Qt5, how can I set a single thread's CPU affinity?
I want to specify a mask of available CPU cores that the thread may run under.
In other words, what is the Qt5 equivalent to Posix thread's pthread_setaffinity_np()
?
Can I do this for threads managed by QThreadPool
?
Usually that sort of things is done by extracting the native thread handle and then doing whatever system specific stuff necessary, as no accepted cross-platform API exists for low level thread management.
Indeed, if we inspect the source for the qthread_unix.cpp
we will see the following:
Qt::HANDLE QThread::currentThreadId() Q_DECL_NOTHROW
{
// requires a C cast here otherwise we run into trouble on AIX
return to_HANDLE(pthread_self());
}
And in qthread_win.cpp
the implementation will differ in the expected way:
Qt::HANDLE QThread::currentThreadId() Q_DECL_NOTHROW
{
return reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId()));
}
So, it is responsibility of the application code to do the proper low level actions pertaining to each platform it is expected to run on.