【问题标题】:SetThreadPriority isn't working even though GetThreadPriority indicates that it is即使 GetThreadPriority 表明它是 SetThreadPriority 也不起作用
【发布时间】:2019-05-26 11:45:43
【问题描述】:

我正在调用 SetThreadPriority(m_hThread, THREAD_PRIORITY_ABOVE_NORMAL) 并在验证是否已设置优先级后立即调用 GetThreadPriority(m_hThread)。根据 GetThreadPriority(),SetThreadPriority() 工作正常,但线程的性能没有改变。

我创建了一个线程来通过动画 GIF 的帧进行动画处理。只要没有其他事情发生,GIF 就会完美地动画,但是一旦进程的主线程繁忙,GIF 就会冻结,直到主线程完成它的工作。

我尝试将线程提升到 THREAD_PRIORITY_HIGHEST 和 THREAD_PRIORITY_TIME_CRITICAL,但没有任何区别。当主线程忙时,GIF 线程仍然冻结。

我已经阅读了所有我可以在网上找到的关于线程优先级的内容,并且没有发现我的代码有任何问题,所以我没有想法。

感谢任何帮助。

m_hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadWrapper, this, CREATE_SUSPENDED, NULL);
DWORD dwThreadPriority1 = ::GetThreadPriority(m_hThread);
BOOL b = ::SetThreadPriority(m_hThread, THREAD_PRIORITY_ABOVE_NORMAL);
DWORD dwThreadPriority2 = ::GetThreadPriority(m_hThread);
ASSERT(THREAD_PRIORITY_ABOVE_NORMAL == dwThreadPriority2);
ResumeThread(m_hThread);

【问题讨论】:

  • 这听起来更像是您遇到了锁定问题,即“主”线程锁定某些资源的时间超过了它需要的时间。

标签: c++ multithreading visual-studio-2013 windows-7


【解决方案1】:

在 Windows 中,所有 UI 都应该在同一个线程中完成。特别是,这应该是创建主窗口的线程

违反此规则可能会产生意想不到的后果。

【讨论】:

  • 同样,任何重要的工作都应尽可能移出 UI线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-17
  • 2020-11-08
  • 2020-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多