实现Qt中的线程间通信 [英] Implementation of Inter Thread Communication in Qt

查看:477
本文介绍了实现Qt中的线程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,无法实现以下情况。我的问题陈述如下:


  1. 我有3个主题。 ThreadCamera从相机抓取帧。

  2. 我不知道ThreadProcess会占用多少时间。ThreadProcess是一个用于处理(用图像/框架上的OpenCV进行图像处理的图像处理)框架和用于显示图像的主GUI线程。以处理图像。因此,我想将图像从ThreadCamera传递到ThreadProcess,对图像进行一些图像处理,并将其传递给主GUI线程进行显示。

  3. 当ThreadProcess处理图像时,T​​hreadCamera应该睡眠。也就是说它不应从相机抓取更多帧。当ThreadProcess完成图像处理任务时,它应该将图像和一些信息传递给主GUI线程。在这之后,只有ThreadCamera应该可以从那个(ThreadCamera)线程中的相机runnig中唤醒并抓取下一帧/图像。

Thanx对我有用[0]丢个板砖[0]引用|举报|编辑删除管理回复次数:1加为好友

  • 如果在处理过程中不想睡觉,如果我丢失了CameraThread捕获的一些帧(这在我睡觉或不睡觉的情况下,我丢失了)相机)

  • 我使用QObject为每个进程(相机进程和图像处理作业)和movetoThread命令,使其运行在

    解决方案。



    方案

    您要查找的是一个简单的发布/订阅模式。在这种类型的分发模式中,当客户端不处于接收图像的状态时,所有消息都会被发送并被客户端丢弃。



    在您的应用程序中:



    像您已经做的那样拥有所有不同的线程(Camera,Processing,Gui)。



    有了CameraThread的peridocally(通过一个qTimer信号,如果你想让它很简单)捕获一个图像,并通过信号/槽连接发送到processingThread。



    processThread正在处理一个图像,它设置一个状态标志(只能是一个成员变量,一个bool会工作)说它当前正在处理一个图像。处理完图片后,您可以设置标志来表示您不处理。



    在处理Things插槽,从CameraThread接收图像,将首先检查您是否正在处理图像。如果你是,你不对信号数据做任何事情,你只是返回。如果你不处理图像,你将存储信号数据并调用过程函数。



    这个工作的诀窍是包括这个函数调用 QCoreApplication :: processEvents())在ProcessingThreads主循环中的处理函数。这将允许你的ProcessingThread处理它获得的任何信号WHILE它正在做一些有用的。



    状态变量检查将允许你'drop'发送给你的所有新图像您正在处理当前的一个,而不排队。


    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. ThreadProcess for processing (doing some image processing with OpenCV on the image/frame grabbed) the frame and main GUI Thread for displaying the image.
    2. I don't know how much time ThreadProcess will take to process an image. So I want to pass the image from ThreadCamera to ThreadProcess , do some image processing on the image and pass it to the main GUI Thread for display.
    3. When ThreadProcess processes the image the ThreadCamera should sleep. I.e. it should not grab further frames from the camera. When the ThreadProcess finishes the image processing task it should pass the image and some information to the main GUI Thread. After this only the ThreadCamera should wake up and grab the next frame/image from the camera runnig in that(ThreadCamera) thread.

    Thanx Guys...after some comments to put Camera and Image Processing job in a single thread i would like to know another point..which is..

    1. What if don't want to sleep the camera while the processing is going on?It does not matter to me if I loose some of the frames grabbed by CameraThread(which in any case I am loosing if i sleep or not sleep the camera)

    I am using QObject for each process(Camera Process and Image Processing job) and movetoThread command to make it run in a particular thread.

    Any insight about the implementation and signal/slot design will be helpful..

    解决方案

    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.

    这篇关于实现Qt中的线程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆