我知道我无法在线程之间与 QTcpSocket 通信,但我找不到我做错了什么。
一旦我向另一个线程中的 QTcpSocket 发送数据发射信号,QSocketNotifier 应该禁用自身以让 QTcpSocket 读取数据。因为我遇到了上述错误,所以它不会被禁用,并且不会发出 readyRead() 信号。
我使用 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);
我将不胜感激任何帮助,谢谢!