有main
功能:
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Worker w;
QObject::connect(&w, SIGNAL(done()), &a, SLOT(quit()), Qt::QueuedConnection);
w.start();
int ret = a.exec();
w.quit();
w.wait();
return ret;
}
还有Worker
的定义:
class Worker : public QThread
{
Q_OBJECT
public:
Worker(QObject *parent=0);
protected:
void run();
protected slots:
void process_request();
private:
int ttl;
Messenger* messenger;
}
Worker::Worker(QObject * parent)
:QThread(parent),
ttl(5),
messenger(new Messenger(this))
{
moveToThread(this);
connect(messenger, SIGNAL(new_message()), SLOT(process_request()), Qt::QueuedConnection);
}
void Worker::finish(){
quit();
messenger->disconnectFromNetwork();
}
void Worker::run(){
messenger->connectToNetwork("somewhere");
exec();
emit done();
}
void Worker::process_request(){
net_message msg;
messenger->recv(msg);
// PROCESSING
messenger->send(msg);
BOOST_LOG_SEV(file_log, severity::notification) << "TTL = " << ttl;
if (--ttl == 0) {
finish();
}
}
好吧,对于冗长的说明感到抱歉。这个想法是 Messenger 存在于主线程中,当它有新消息时会戳 Worker,而 Worker 只存在一定数量的消息,之后它会停止并关闭整个应用程序。
但是有一个问题:日志文件有行TTL = -1
,和TTL = -2
,等等。它不应该,我能想到的唯一原因是它quit()
并没有完全结束事件循环:它允许在从exec()
. 是这样吗?如果“否”,那么可能导致这种行为的原因是什么?