我有一个这样的线程子类
class MyThread : public QThread
{
public:
MyThread (void);
~MyThread (void);
void run();
void stop(); // close server and terminate thread
public slots:
void slotCloseServer();
signals:
void signalCloseServer();
private:
QTcpServer* m_pServer;
};
和run()
整体类定义概述如下
MyThread::MyThread()
{
connect(this, SIGNAL(signalCloseServer()), this, SLOT(slotCloseServer()));
}
void MyThread::run()
{
m_pServer = new QTcpServer();
if(m_pServer->listen(QHostAddress(QHostAddress::LocalHost), 8888) == 0)
{
qCritical("Server listen failed.");
}
else
{
exec();
}
}
void MyThread::stop()
{
emit signalCloseServer();
quit(); // terminate thread
}
void MyThread::slotCloseServer()
{
if (m_pServer && m_pServer->isListening())
{
m_pServer->close();
}
}
现在从主线程开始,我想调用stop()
它,以便它会向线程实例发出信号以关闭服务器并自行终止,但它永远不会到达slotCloseServer()
并且监听端口不会被释放。
关于如何在终止线程之前释放服务器套接字的任何想法?
谢谢,