title: Access to two variables safely when an interrupt might occur between them
body: <p>First of all I'd welcome edits to the title of this question, I couldn't think how to word it better but I'm not too happy with what I came up with.</p>
  
  <p>This is a question about concurrency, my application is on a microcontroller in C but I don't think that matters a great deal.</p>
  
  <p>I have an interrupt routine which can change the values of two global variables. I have some main code which can read those variables. But it must get get consistent values from both which means I can't read one and then read the other because the interrupt might occur between the two and change them both, leaving me having read one from one set and one from the other.</p>
  
  <p>Normally I would just disable the interrupt for the tiny part of code that reads both variables but I can't do this because the interrupt needs to be called at exactly the right time with no "jitter" in calls. 4 or 5 instructions to read and store the variables will lead to too much jitter in interrupt timing. (I'm generating PAL video in the interrupt so any jitter in timing will lead to visible movement of pixels on the screen). </p>
  
  <p>I could rearrange the code to do this in a different way so that the same interrupt isn't responsible for the two things, and long term I'll probably do this, but it's significant work to do so. The question has become interesting to me anyway now as a 'puzzle' even if I recode it later to avoid the situation.</p>
  
  <p><i><b>So what I'm asking is, is there any way I can read both variables and ensure they are in a consistent state without disabling interrupts?</b></i></p>
  
  <p>It doesn't matter if I get the value before or after the interrupt occurs as long as both values come from the same place.</p>
  
  <p>I've thought about having a separate "version number" count to read to ensure that each variable is the same version that is read, but that just makes the problem worse as now I have 4 variables to read.</p>