Qt中的类设计用于线程间通信 [英] Class Design in Qt for inter thread communication

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

问题描述

问题陈述:使用摄像机跟踪对象,并相应地移动摄像机的方位角和高度。

Problem statement : to track an object with a camera and move the camera in azimuth and elevation accordingly.

/ strong>:摄像机获取对象的图像...每帧摄像机被处理以找到对象(应该被跟踪...),并且在每个帧中产生的信息被传递到机械装置(云台...)在平移和倾斜中移动相机...

Process : camera acquires images of the object....each frame of camera is processed to find the object(which is supposed to be tracked...) and the information generated in each frame is passed to the mechanical device (gimbal...) to move the camera in pan and tilt...

设计:主要是在线程和相机中运行gimbal在2其他线程...在相机线程生成的信息传递到云台线程和相同的线程(相机线程...)显示跟踪的图像...

Design : the main Gui is run in a thread and camera and gimbal in 2 other thread...info generated in camera thread is passed on to gimbal thread and the same thread(camera thread...) displays the tracked image...

CONSTRAINT:显示速度快于云台的移动速度 ...因此在每个帧中...

CONSTRAINT: display rate is faster than move rate of gimbal...so in each frame...


    < <>
  1. gnbal线程暂停更长的时间(<10 ms) 100 ms),以给予机械部件移动的时间,并确保没有新的更新到万向节,直到它完成其运动。

下图显示了Qt中的类和信号和插槽连接的设计。

the following diagram shows the design of classes and signal and slot connections in Qt..

请尽量提出更好的方法来解决问题...

please suggest any better way to go about the problem if possible...

ERROR :有时会收到错误...

ERROR: sometimes i get an error...

QObject :: killTimers:timers不能从另一个线程停止

推荐答案

对我来说,你应该避免调用线程之间的函数
这里是一个想法,这是我会做的安全交换很多数据的方式。

To me you should just avoid to call functions like that between the thread, here is an idea, that is the way i would do to exchange safely a lot of data.

,你应该有状态机与线程访问共享可锁定的变量(指向你的主线程指向ping或乒乓缓冲区的指针+你的锁的互斥量)

and you should have state machine with both thread accessing to a shared lockable variable (a pointer to use for your main thread that points to the ping or the pong buffer + a mutex for your lock)

这样你可以改变你的方式使用数据,显示应该总是使用相同的指针。

this way you can change the way you use data, the "display" should use always the same pointer. It should acquire the lock when starting to read and releasing after.

相机应该写入另一个缓冲区
相机写入后,相机线程应该尝试获取互斥的锁,如果它获取它(即另一个线程没有读取另一个bufer),则相机线程应该采取锁定并更改由主线程使用的指针指向的缓冲区)

The camera should write in the other buffer After the camera writed, the camera thread should try lo acquire the lock for the mutex and IF it acquires it (ie the other thread is not reading the other bufer), then the camera thread should take the lock and change the buffer pointed by the pointer used by the main thread)

这样,你在线程之间没有进行互斥的通信。

This way you have no communication between threads just the mutual exclusion.

最大的风险是能够从相机线程获取锁,这里是一个解决方案 - >
为了限制锁定时间,我建议你在每个线程中有一个第二个缓冲区,因此锁定时间将被减少到 memcpy持续时间。
向结构中添加一个bool,即如果你的相机更新了数据。

The biggest risk is to never being able to acquire the lock from the camera thread, here is a solution -> In order to limit the lock time, I advise you to have a second buffer in each thread so the lock time would be reduced to the "memcpy" duration. Add a bool to the structure, thats says if your camera updated the data or not.

如果是,主线程复制数据。
(并且相机在这段时间内不能交换指针),但一旦完成,你的主线程也将bool重置为false。
直到这个bool回到true,你不需要锁定来复制数据。
(现在这是免锁定的交换)
注意,这只是因为读取bool是一个原子操作。

If yes, the main thread copy the data. (and the camera can't swap pointers during this time) but once it is done, you main thread also reset the bool to false. And until this bool is back to true you don't need to lock to copy data. (now this is lock free exchange) Note that this is true only because reading the bool is an atomic action.

我希望它有帮助

(对不起我的英语)

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

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