QSocketNotifier:无法从另一个线程禁用套接字通知程序

问题描述 投票:0回答:2

我知道我无法在线程之间与 QTcpSocket 通信,但我找不到我做错了什么。

一旦我向另一个线程中的 QTcpSocket 发送数据发射信号,QSocketNotifier 应该禁用自身以让 QTcpSocket 读取数据。因为我收到上述错误,所以它不会被禁用,并且不会发出 readRead() 信号。

我使用 QEventLoop 来实现阻塞机制(同步通信 - ModBus 协议),而不锁定主循环。我无法使用 waitReadyRead() 因为我过去在使用该函数时遇到了问题(超时):

https://bugreports.qt-project.org/browse/QTBUG-24451

https://bugreports.qt-project.org/browse/QTBUG-14975

有人可能会好奇为什么我要设置这么多线程:

Thread1:每次新通信都会创建一个新线程,并用“全局”互斥锁锁定它,以确保没有其他函数会执行并行通信。在该线程内部 - 在准备和锁定互斥体之后,我通过 Thread2 中的 Qt::QueuedConnection 信号与 QTcpSocket 进行通信(与设备持续通信)。

Thread2:创建QTcpSocket对象并移动到线程。连接设备后,我不想关闭套接字。因为我阻塞了主事件循环,所以我需要其他事件循环来运行来自 QTcpSocket 的信号。

代码:

void someFunctionThatWantToSendData( void ) // Main Thread
{
    ElfKernel::KernelSingleton::Instance().getGUIcontrol()->getCtrlUnitPtr()->execModbusCommand( someParameters, someCommunicationFunction );
}

ElfKernel::FrameReceived ControlUnit::execModbusCommand( std::list<unsigned int> paramsToCommand, FunctionStringList execCommand ) // Main Thread
{
    ModbusCommandReply* mcr = new ModbusCommandReply(); // THREAD_1!!!!!! this object run in a Thread -> ModbusCommandReply : public QThread
    auto timeout = ElfKernel::CommunicationManagerSingleton::Instance().getTimeout();
    mcr->execute( paramsToCommand, execCommand );  // executes THREAD_1
    bool hasFinishedWithoutTimeout = mcr->wait(timeout);  // waiting for THREAD_1 to finish
    FrameReceived ret = mcr->getData();
    delete mcr;
    return ret;
}

// this method is executed after preparations done by: mcr->execute( paramsToCommand, execCommand );
ElfKernel::FrameReceived CommunicationManager::getData( std::string data )  // THREAD_1
{
    emit signalTcpSocketWriteData( data );
    loop.exec();    // QEventLoop declared in a header; program loops until QTcpSocket::readyRead() signal will hit the slot TcpSocketClient::readyRead()
    FrameReceived fr = tcpSocket->readDataString();
}

// executed by: emit signalTcpSocketWriteData( data );
void TcpSocketClient::writeDataSlot( std::string data ) // THREAD_2!!!!!
{
    tcpSocket_->write( data );
    tcpSocket_->flush();
}

void TcpSocketClient::readyRead( void ) // should be executed in THREAD_2 but signal readyRead() never arrives here because a get error on Output: "QSocketNotifier: socket notifiers cannot be disabled from another thread"
{
    ElfKernel::CommunicationManagerSingleton::Instance().loop.quit();   // breaks loop.exec() in CommunicationManager::getData()
}

// constructor
CommunicationManager::CommunicationManager( void )  // Main Thread
{
    tcpSocket = new TcpSocketClient();
    tcpSocketThread = new QThread();
    tcpSocket->moveToThread( tcpSocketThread );
    QObject::connect( tcpSocketThread, SIGNAL(started()), tcpSocket, SLOT(prepare()) );
    tcpSocketThread->start();   // CREATED THREAD_2!!!!!

    QObject::connect(this, SIGNAL(signalTcpSocketWriteData(std::string)), tcpSocket, SLOT(writeDataSlot(std::string)), Qt::QueuedConnection );
    QObject::connect(this, SIGNAL(signalTcpSocketReadData(std::string *)), tcpSocket, SLOT(readDataSlot(std::string *)), Qt::QueuedConnection);

    // and other stuff
}

// constructor
TcpSocketClient::TcpSocketClient(QObject *parent)   // Main Thread - before it is moved to THREAD_2
{
    parent;

    //prepare();    // it is executed after signal QThread::started() is emitted
}

// method run after thread started
void TcpSocketClient::prepare( void )   // THREAD_2
{
    tcpSocket_ = new QTcpSocket(this);
    QObject::connect(tcpSocket_, SIGNAL(disconnected()), this, SLOT(hostHaveDisconnected()));
    QObject::connect(tcpSocket_, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

正如我所说,插槽 TcpSocketClient::readyRead() 将不会被调用,并且我在输出上收到消息:“QSocketNotifier:无法从另一个线程禁用套接字通知程序”。

如果我替换此代码:

ElfKernel::FrameReceived ControlUnit::execModbusCommand( std::list<unsigned int> paramsToCommand, FunctionStringList execCommand ) // Main Thread
{
    ModbusCommandReply* mcr = new ModbusCommandReply(); // THREAD_1!!!!!! this object run in a Thread -> ModbusCommandReply : public QThread
    auto timeout = ElfKernel::CommunicationManagerSingleton::Instance().getTimeout();
    mcr->execute( paramsToCommand, execCommand );  // executes THREAD_1
    bool hasFinishedWithoutTimeout = mcr->wait(timeout);  // waiting for THREAD_1 to finish
    FrameReceived ret = mcr->getData();
    delete mcr;
    return ret;
}

与:

ElfKernel::FrameReceived ControlUnit::execModbusCommand( std::list<unsigned int> paramsToCommand, FunctionStringList execCommand ) // Main Thread
{
    FrameReceived ret = execCommand( paramsToCommand );  // Main Thread
    return ret;
}

因此,程序不会被推送到线程,而是在主线程中运行(唯一的区别!)然后一切都正常工作-readyRead插槽工作循环中断,我得到了数据。 我需要将 ModbusCommandReply 作为线程运行,因为我在 mcr->execute 中设置了 mutex.lock() ,因此每当程序的其他部分运行方法 execModbusCommand 时,它都会停止,直到其他线程释放资源来建立通信。

我不明白为什么它说“QSocketNotifier:无法从另一个线程禁用套接字通知程序” - 我在线程启动后通过将新对象放在堆上来在 THREAD_2 内设置 QTcpSocket: tcpSocket_ = new QTcpSocket(this);

我将不胜感激任何帮助,谢谢!

qt sockets networking qtcpsocket
2个回答
5
投票

在主线程中我已经建立了连接:

tcpSocket->connect( ... );

在主线程中而不是在 tcpSocketThread 中生成子对象。

还建议这样做:

tpcSocket->setParent(0);
// before
tcpSocket->moveToThread( tcpSocketThread );

0
投票

我也面临类似的问题 我的 C++ QT 应用程序是在 QT4 上构建的,并且所有 QTcpSocket 实现都按预期工作 当我迁移到 QT5 时,我收到“无法从另一个线程禁用套接字通知程序”问题

© www.soinside.com 2019 - 2024. All rights reserved.