我试图在显示QT窗口时在单独的线程中运行我的ASIO网络服务:
QTServer::QTServer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
try
{
asio::io_service io_service;
asio::ip::tcp::endpoint endpoint(asio::ip::tcp::v4(), std::atoi("1337"));
networkService srv(io_service, endpoint);
t = std::thread{ [&io_service](){ io_service.run(); } };
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
}
t
被定义为MainWindow的成员:
class QTServer : public QMainWindow
{
Q_OBJECT
public:
QTServer(QWidget *parent = 0);
~QTServer();
private:
Ui::QTServerClass ui;
std::thread t;
};
所以,我得到了:
调试错误-Abort()已被调用
我知道在调用join()之前销毁线程时通常会发生此错误。但是,由于线程是窗口的成员变量,我不知道为什么会这样。