Implementation of Inter Thread Communication in Qt

前端 未结 1 354
闹比i
闹比i 2021-01-22 15:59

I am having problem to implement the following scenario. My problem statement goes like this:

  1. I have 3 threads. ThreadCamera for grabbing frames from a camera. Thr
1条回答
  •  梦毁少年i
    2021-01-22 16:23

    What you're looking for is a simple "Publish/Subscribe" pattern. In this type of 'distribution' pattern, all messages are sent and just dropped by the client when it's not in a state to receive images

    I would implement this as the following in your application:

    Have all separate threads (Camera,Processing,Gui) like you already do.

    Have the CameraThread peridocally (through a qTimer signal maybe if you want to make it simple) capture an image and sent it over a signal/slot connection to the processingThread.

    When the processingThread is processing an image, it sets a state flag (can just be a member variable, a bool would work) to say that its currently processing an image. When you're done processing the image you set the flag to say that you're not processing.

    In the processingThreads slot, which receives the images from the CameraThread, you will first check to see if you're currently processing an image. If you are, you do not do anything with the signals data and you just return. If you are not processing an image, you will store the signals data and call the process function.

    The trick to making this work is to include this function call (QCoreApplication::processEvents()) in your ProcessingThreads main loop in the processing function. This will allow your ProcessingThread to process any signals it gets WHILE it's doing something useful.

    The state variable checking will allow you to 'drop' all new images sent to you while you're processing a current one and not queue them up.

    0 讨论(0)
提交回复
热议问题