上来先来一段代码:
QSemaphore semaphore;
QtConcurrent::run( serverThreadPool_.data(), [ &semaphore, this ]()
{
QEventLoop eventLoop;
#if 0 //此处注释掉的代码是原先的。
if(QObject::connect( this, &AbstractManage::readyToClose, &eventLoop, &QEventLoop::quit )){
qDebug() << "收到关闭信号";
}
#else
QObject::connect(this,&AbstractManage::readyToClose,[&](){
qDebug() << "收到关闭信号";
eventLoop.exit(0);
});
#endif
if ( !this->onStart() )
{
semaphore.release( 1 );
}
semaphore.release( 1 );
eventLoop.exec(); // 收到信号后,退出
this->onFinish();
} );
问题是这样子的, #if 0代码段连接readyToClose到QEventLoop的quit槽,看上去没什么问题。但是等到发射readyToClose的时候,eventLoop.exec死活结束不掉。 肉眼看上去没什么问题。 怀疑两点,1. 要么线程内的槽收不到信号; 2. 要么收到了信号,但是执行有问题。
第一点,显然不成立,线程内已经增加了事件循环,应该不会有问题。
第二点,改成了#else的代码方式,信号是收到了的,在eventLoop.exit(0)后,eventLoop也可以正常退出。
看来是QEventLoop.quit槽有问题。官方文档说:
[slot] void QEventLoop::quit()
Tells the event loop to exit normally.
Same as exit(0).
See also QCoreApplication::quit() and exit().
实际上还是有所区别的,不想去研究Qt源码了,就这样子吧。
本文暂时没有评论,来添加一个吧(●'◡'●)