MFC和AfxBeginThread的线程调度问题
我正在使用 AfxBeginThread 在 MFC 中创建一个工作线程,但该线程未得到调度。这是代码:
CWinThread* worker = AfxBeginThread(initialUpdateWorkerThread, this);
DWORD dwExitCode = 0;
while(GetExitCodeThread(worker->m_hThread, &dwExitCode))
{
if(dwExitCode != STILL_ACTIVE)
break;
::Sleep(100);
}
当我运行这个循环时,这个循环只是活锁,因为initialUpdateWorkerThread永远不会被调用(我在它的顶部放置了断点和消息框),所以dwExitCode始终是STILL_ACITVE。但是,如果我在循环之前但在 AfxBeginThread 之后调用 AfxMessageBox,则调用该函数。这让我觉得不知何故我没有调用正确的函数来安排线程,但是对 AfxMessageBox 的调用导致它被安排。
如何强制调度线程?我认为睡眠可以做到这一点,但在这种情况下似乎没有。
I'm creating a worker thread in MFC with AfxBeginThread, but the thread is not getting scheduled. Here's the code:
CWinThread* worker = AfxBeginThread(initialUpdateWorkerThread, this);
DWORD dwExitCode = 0;
while(GetExitCodeThread(worker->m_hThread, &dwExitCode))
{
if(dwExitCode != STILL_ACTIVE)
break;
::Sleep(100);
}
When I run this, this loop just livelocks because initialUpdateWorkerThread is never called (I've put break points and message boxes at the top of it) so dwExitCode is always STILL_ACITVE. But if I put in a call to AfxMessageBox before the loop but after AfxBeginThread then the function is called. This makes me think that somehow I'm not calling the right function to get the thread scheduled, but a call to AfxMessageBox causes it to get scheduled.
How can I force the thread to be scheduled? I would think sleep would do that, but in this case it doesn't seem to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的工作线程可能正在尝试向主线程发送消息,但由于您没有在主线程上处理消息,因此工作线程只是等待。您可以通过简单地进入调试器来查看工作线程正在做什么来确认这一点。
Your worker thread is probably trying to send your main thread a message, but since you aren't processing messages on on the main thread, the worker thread simply waits. You can confirm this by simply breaking into the debugger to see what the worker thread is doing.