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

前端 未结 3 1306
南方客
南方客 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:06

    Unless it's strictly necessary for some reason, I'd get rid of the QWaitCondition. Instead, when the have enqueueMessage() emit a (Qt) signal after it has appended new data to the QQueue, and have your worker thread receive that signal (along with whatever other signals it needs to receive) in the usual Qt way. Then your problem goes away, with no timeouts or other hackery needed.

    (optional optimization: have the serial port only emit the signal if the QQueue was empty before it added the new data, and have the main thread's corresponding slot read from the QQueue until the QQueue is empty -- that can cut down on the number of signals that need to be sent)

提交回复
热议问题