简体   繁体   English

如何在Qt5中设置线程的CPU关联性?

[英]How can I set a thread's CPU affinity in Qt5?

Working with threads in Qt5 , how can I set a single thread's CPU affinity ? Qt5中使用线程,如何设置单个线程的CPU关联性

I want to specify a mask of available CPU cores that the thread may run under. 我想指定线程可能在其下运行的可用CPU内核的掩码。

In other words, what is the Qt5 equivalent to Posix thread's pthread_setaffinity_np() ? 换句话说,与Posix线程的pthread_setaffinity_np()等效的Qt5是什么?

Can I do this for threads managed by QThreadPool ? 我可以对QThreadPool管理的线程执行此QThreadPool吗?

Qt doesn't provide any public APIs for setting processor affinity. Qt不提供任何用于设置处理器相似性的公共API。 There's definitely no way to do this for a QThreadPool , which doesn't even provide an API for accessing the QThread s within the pool. 对于QThreadPool绝对没有办法做到这QThreadPool ,它甚至不提供用于访问池中QThread的API。

For explicitly-created QThread objects, you could try using the QThread::getCurrentThreadId() function to return a native "handle" to the thread, and then pass that to your system's thread-management library calls. 对于显式创建的QThread对象,可以尝试使用QThread::getCurrentThreadId()函数将本机“句柄”返回给线程,然后将其传递给系统的线程管理库调用。 But I would strongly recommend against this. 但是我强烈建议不要这样做。 The docs explicitly state: 该文档明确指出:

Warning: The handle returned by this function is used for internal purposes and should not be used in any application code. 警告:此函数返回的句柄仅供内部使用,不应在任何应用程序代码中使用。

If you're building for a specific platform, you might be able to mix calls to your OS's library (eg, pthreads ), but that will not be portable and I have no idea if it will work. 如果要针对特定​​平台进行构建,则可以混合对操作系统库的调用(例如pthreads ),但这将无法移植,并且我不知道它是否可以工作。

Your best bet is probably to manage the CPU affinity of the whole application from the command-line. 最好的选择可能是从命令行管理整个应用程序的CPU关联性。 If you're on Linux, taskset is the way to go. 如果您使用的是Linux,则taskset For Windows, take a look at this SO answer . 对于Windows,请查看此SO答案 Unfortunately, Apple seems pretty determined to prevent users from setting thread affinity, at least from the command line. 不幸的是,Apple似乎决心要阻止用户至少在命令行中设置线程亲和力。

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. 通常,这类事情是通过提取本机线程句柄然后执行任何必要的系统特定操作来完成的,因为不存在用于低级线程管理的可接受的跨平台API。

Indeed, if we inspect the source for the qthread_unix.cpp we will see the following: 确实,如果我们检查qthread_unix.cpp的源,我们将看到以下内容:

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: qthread_win.cpp ,实现将以预期的方式有所不同:

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. 因此,应用程序代码有责任对预期在其上运行的每个平台进行适当的低级操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM