title: Synchronizing a thread with two running threads, c++, windows
body: <p>I am a newbie in multithread programming and this is my first post here! So please bear with me.  </p>
  
  <p>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. Let's say the first camera outputs <em>x1</em> and <em>y1</em>, the second camera outputs <em>x2</em> and <em>y2</em> and these should be used to estimate <em>x</em>, <em>y</em>, and <em>z</em>. </p>
  
  <p>I was thinking of defining <em>x1</em>, <em>y1</em>, <em>x2</em> and <em>y2</em> as global variables so that they are easily accessible for the third thread. 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 <em>x1</em>, <em>y1</em>, <em>x2</em> and <em>y2</em>. It only uses these values to obtain <em>x</em>, <em>y</em> and <em>z</em>. But if the value <em>x1</em> and <em>y1</em> is updated and <em>x2</em> and <em>y2</em> are not yet updated, I want the third thread to pause until the <em>x2</em> and <em>y2</em> is updated. Or in other words, I want the third thread to use <em>x1</em>, <em>y1</em>, <em>x2</em> and <em>y2</em> that are ideally obtained at a time point <em>t</em>, or during a very short period of time. </p>
  
  <p>I appreciate any suggestions on how to approach this problem. I am thinking of using 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 <em>t1</em> and <em>t2</em> (system time) from thread 1 and 2 respectively, and compare them in the third thread? 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.   And as a result, thread 3 would never find <em>x1</em>, <em>y1</em>, <em>x2</em> and <em>y2</em> which are obtained very close in time!</p>
  
  <p>NOTE: I am using windows 7, visual studio 2010, programming language C++.</p>