QThread混乱
我在 Windows 7 Ultimate(32 位) 计算机上使用 Qt 4.7.0(32 位)。我可能从 4.0 或 4.2 版本开始使用 Qt,并且几乎使用了所有 4.xy 版本。
最近,使用 Qt 4.7 我遇到了麻烦。
我用一些旧版本的 Qt 编写了一个多线程应用程序。我忘记了版本,但我正确编译和运行的最后一个版本可能是 4.5.x 或 4.6.x。 线程部分在4.7中似乎无法正常工作,或者我误解了一些东西。问题是这样的:
主线程启动 thinker
线程。以下是 thinker
线程的 run()
函数:
void ThinkerThread::run()
{
_threads_running = NSUBTHINKERS;
// ...
_sub_thinker[0].start();
// ...
_sub_thinker[1].start();
exec();
}
_sub_thinker
的 finished()
信号与所有 _sub_thinker
的 ThinkerThread::subThinkerFinished()
插槽。当所有 _sub_thinker
完成时,ThinkerThread::subThinkerFinished()
调用 quit()
。
还有一个地方调用了quit()
:
void ThinkerThread::tryKill()
{
for (int i = 0; i < NSUBTHINKERS; i++)
_sub_thinker[i].tryKill();
quit();
}
从主线程,执行如下:
- thinker.tryKill() [注意:在第一次运行时,thinker 没有运行。]
- thinker .start()
thinker
的 finished()
信号与 autoMove()
连接。
对于 Qt 的所有早期版本,在从 ThinkerThread::subThinkerFinished()
调用 thinker::quit()
后调用 autoMove()
。在 4.7 中,我看到在第一次调用 thinker.tryKill()
之后调用了 autoMove()
,即使线程没有运行。
有什么想法吗?
当然,我可以在 ThinkerThread::tryKill()
内部检查 thinker
是否正在运行。但我想知道为什么会发生这种情况。
谢谢。
I'm using Qt 4.7.0 (32 bit) on Windows 7 Ultimate (32 bit) machine. I've been using Qt probably from version 4.0 or 4.2 and I've used almost all 4.x.y releases.
Recently, with Qt 4.7 I've faced trouble.
I've written a multithreaded application in some older version of Qt. I've forgotten the version but probably the last version where I compiled and ran properly is 4.5.x or 4.6.x. The threaded part doesn't seem to be working correctly in 4.7, or I misunderstood something. Here is the problem:
The main thread starts thinker
thread. Following is the run()
function of thinker
thread:
void ThinkerThread::run()
{
_threads_running = NSUBTHINKERS;
// ...
_sub_thinker[0].start();
// ...
_sub_thinker[1].start();
exec();
}
The _sub_thinker
's finished()
signal is connected with ThinkerThread::subThinkerFinished()
slot for all _sub_thinker
s. When all _sub_thinker
s finish, ThinkerThread::subThinkerFinished()
calls quit()
.
There is another place where quit()
is called:
void ThinkerThread::tryKill()
{
for (int i = 0; i < NSUBTHINKERS; i++)
_sub_thinker[i].tryKill();
quit();
}
From main thread, the execution is like as follows:
- thinker.tryKill() [Note: On very fist run, thinker wasn't running.]
- thinker.start()
thinker
's finished()
signal is connected with autoMove()
.
For all previous versions of Qt, autoMove()
was called after thinker::quit()
is called from ThinkerThread::subThinkerFinished()
. In 4.7 I'm seeing that autoMove()
is called after the very first call of thinker.tryKill()
, even though the thread wasn't running.
Any idea?
Of course I can check inside ThinkerThread::tryKill()
if thinker
is running or not. But I'd like to know why is this happening.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现每次调用
QThread::quit()
时都会发出QThread::finished()
,无论QThread
的状态如何(即运行/不运行)。以前版本的 Qt 并非如此。I've found that
QThread::finished()
is emitted every timeQThread::quit()
is called irrespective of the state ofQThread
(i.e., running/not running). It didn't the case for previous versions of Qt.