简体   繁体   English

将一个线程与两个正在运行的线程(C ++,Windows)同步

[英]Synchronizing a thread with two running threads, c++, windows

I am a newbie in multithread programming and this is my first post here! 我是多线程编程的新手,这是我在这里的第一篇文章! So please bear with me. 所以,请忍受我。

I have two cameras which I would like to track an object simultaneously (two independent threads) and output its position to the main function or perhaps a third thread. 我有两个摄像机,我想同时跟踪一个对象(两个独立的线程),并将其位置输出到主要功能或第三个线程。 Using the position of the object from each camera, the 3D position of the object should then be calculated. 使用每个摄像机的对象位置,然后应计算对象的3D位置。 Let's say the first camera outputs x1 and y1 , the second camera outputs x2 and y2 and these should be used to estimate x , y , and z . 假设第一个摄像头输出x1y1 ,第二个摄像头输出x2y2 ,这些应用于估计xyz

I was thinking of defining x1 , y1 , x2 and y2 as global variables so that they are easily accessible for the third thread. 我当时正在考虑将x1y1x2y2定义为全局变量,以便第三个线程可以轻松访问它们。 But the problem is that the third thread should be synchronized with the first 2 threads. 但是问题在于,第三个线程应该与前两个线程同步。 The third thread doesn't change the content of x1 , y1 , x2 and y2 . 第三个线程不会更改x1y1x2y2的内容 It only uses these values to obtain x , y and z . 它仅使用这些值来获得xyz But if the value x1 and y1 is updated and x2 and y2 are not yet updated, I want the third thread to pause until the x2 and y2 is updated. 但是,如果值x1y1已更新,而x2y2尚未更新,我希望第三个线程暂停直到x2y2更新。 Or in other words, I want the third thread to use x1 , y1 , x2 and y2 that are ideally obtained at a time point t , or during a very short period of time. 换句话说,我希望第三个线程使用理想情况下在时间点t或在很短的时间内获得的x1y1x2y2

I appreciate any suggestions on how to approach this problem. 我感谢有关如何解决此问题的任何建议。 I am thinking of using CreateThread(). 我正在考虑使用CreateThread()。 Is there an easier way? 有更容易的方法吗? Is it a good idea to use global variable in multithread programming? 在多线程编程中使用全局变量是一个好主意吗? Is it a good idea to output global variables t1 and t2 (system time) from thread 1 and 2 respectively, and compare them in the third thread? 分别从线程1和2输出全局变量t1t2 (系统时间)并在第三个线程中进行比较是个好主意吗? One problem with this approach might be that since both threads 1 and 2 are doing the same thing, it takes let's say T second for them to complete their task and therefore there is always a fixed lag between these threads which might be longer that what we want. 这种方法的一个问题可能是,由于线程1和2都在做相同的事情,因此,假设他们花费T秒来完成任务,因此这些线程之间始终存在固定的滞后时间,该滞后时间可能长于我们想。 And as a result, thread 3 would never find x1 , y1 , x2 and y2 which are obtained very close in time! 结果,线程3将永远找不到在很近的时间内获得的x1y1x2y2

NOTE: I am using windows 7, visual studio 2010, programming language C++. 注意:我正在使用Windows 7,Visual Studio 2010和C ++编程语言。

Thread 1 can SetEvent(event1) and thread 2 can SetEvent(event2). 线程1可以SetEvent(event1),线程2可以SetEvent(event2)。 Thread 3 uses WaitForMultipleObjects to wait until both events have been set. 线程3使用WaitForMultipleObjects等待直到两个事件都被设置。 WaitForMultipleObjects suspends the calling thread and then returns when both events are set. WaitForMultipleObjects挂起调用线程,然后在同时设置两个事件时返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM