如何使用 Ryzen 7 修复 Windows 上 Qt 5.9 线程的处理器亲和性

How to fix processor affinity for Qt 5.9 Thread on Windows with Ryzen 7

我一直在使用 OpenSceneGraph-3.4.0 和 Qt 5.9 的 GUI 为我的硕士论文开发一个程序(否则在 Visual Studio 2015 年和 2017 年)。在工作中一切正常,但现在我在家里有了一台新电脑,我试图得到它 运行。

但是,当我为查看器调用 frame() 方法时,我在 setProcessorAffinity(unsigned int cpunum) 处的 QtThread.cpp 中收到读取访问冲突,特别是在以下行中:

QtThreadPrivateData* pd = static_cast<QtThreadPrivateData*>(_prvData);

这里是完整的函数(QtThread.cpp是OSG的OpenThreads的一部分):

// Description:  set processor affinity for the thread
//
// Use: public
//
int Thread::setProcessorAffinity(unsigned int cpunum)
{
    QtThreadPrivateData* pd = static_cast<QtThreadPrivateData*>(_prvData);
    pd->cpunum = cpunum;
    if (!pd->isRunning) return 0;

    // FIXME:
    // Qt doesn't have a platform-independent thread affinity method at present.
    // Does it automatically configure threads on different processors, or we have to do it ourselves?
    return -1;
}

OSG 中的查看器设置为 osgViewer::Viewer::SingleThreaded,但如果我删除该行,我会在 GraphicsWindowQt.cpp(它是 OsgQt 的一部分)中出现错误 "Cannot make QOpenGLContext current in a different thread",所以这可能是一条死胡同。


编辑澄清

我在 osgViewer::Viewer 对象上调用 frame()。 在这个函数中,查看器调用realize()(这是查看器class的一个函数)。 在那里 setUpThreading() 被调用(这是 Viewer Base class 的一个函数)。 这又调用 OpenThreads::SetProcessorAffinityOfCurrentThread(0) 在那里,执行了以下代码:

Thread* thread = Thread::CurrentThread();
if (thread)
    return thread->setProcessorAffinity(cpunum);

thread(在第一行之后)有一个值 0x00000000fdfdfdfd,这对我来说看起来像是一个错误。 无论如何,最后一个电话是我在原始问题中发布的电话。


我什至不知道从哪里开始解决这个问题。我想,这是一些与处理器相关的问题。我的处理器是 Ryzen 7 1700(在工作中它是 Intel i7 3770k),所以这可能有帮助。 否则,在家里我使用 Windows 10,而在工作中它是 Windows 7.

如果有任何帮助,我将不胜感激。

所以最后,这似乎是 OpenThreads 的问题(因此是 OpenSceneGraph 部分,我对此无能为力)。将 cmake 用于 OpenSceneGraph 源时,有一个选项 "BUILD_OPENTHREADS_WITH_QT" 需要禁用 .

我在 this thread in the OSG forum 中找到了解决方案,非常感谢这个人。