How to implement a QThread that runs forever{} with a QWaitCondition but still needs to catch another Slot while doing that

前端 未结 3 1286
南方客
南方客 2021-01-12 13:29

I implemented a class that can write data to a serial port via a QQueue and read from it by a slot. I use QAsyncSerial for this which in turn uses boost::asio with a callbac

3条回答
  •  不思量自难忘°
    2021-01-12 14:19

    1) Create slot in your thread, for example onMessageReady(), which will do the job.

    2) Create a signal indicates that new message ready, and emit it each time you creating a new message.

    3) Connect them using QueuedConnection and call your thread's exec function.

    This won't block your thread, as WaitforObject does, and you will handle all incoming signals.

    something like this:

    SerialPortHandler: public QThread
    {
      Q_OBJECT
    ...
    signals:
        void sNewMessageReady();
    slots:
        void onNewMessageReady();
        void serialSlotReceivedData(QByteArray);
    };
    
    SerialPortHandler::SerialPortHandler(SerialPort serialPort, QObject *parent) : QThread(parent), serialPort(serialPort)
    {
        m_enqueueMessageMutex = new QMutex();
        m_messageQueue = new QQueue();
        serial.open(serialPort.deviceName(), 2400);
        connect(&serial, SIGNAL(dataReceived(QByteArray)), this, SLOT(serialSlotReceivedData(QByteArray)));
        connect(this, SIGNAL(sNewMessageReady()), this, SLOT(onNewMessageReady()),Qt::QueuedConnection);
    }
    
    void SerialPortHandler::enqueueMessage(BaseMessage *msg)
    {
        QMutexLocker locker(m_enqueueMessageMutex);
        m_messageQueue->enqueue(msg);
        emit sNewMessageReady();
    }
    
    
    void SerialPortHandler::onNewMessageReady()
    {
        QMutexLocker locker(m_enqueueMessageMutex);
        BaseMessage *msg = m_messageQueue->dequeue();
        serial.write(msg->encodeForWriting());
    }
    

    after all simply call thread's exec() method, you don't need to reimplement run() and to use QWaitCondotion at all.

提交回复
热议问题