title: Example C Function using volatile variables
body: <p>for a paper I'm looking for an <em>real-life</em> C function which uses volatile variables.  That in itself is not hard to find, but I am looking for a function in which the value of the volatile variable must change <strong>during</strong> the course of the execution of the function, for a particular branch of the function to be reached.  Something like this:</p>
  
  <pre><code>typedef struct {
      unsigned       :6;
      unsigned FLAG1 :1;
      unsigned FLAG2 :1;
  } __attribute__ ((packed)) REGISTER;
  
  volatile REGISTER * io_ = 0x1234;
  
  int write_to_io()
  {
      while (io_-&gt;FLAG1) {};
      //io_-&gt;FLAG1 is now 0
  
      io_-&gt;FLAG2 = 1;
  
      sleep(10);
  
      if (io-&gt;FLAG1)  
          return 1;  //io-&gt;FLAG1 has changed from 0 to 1
      else
          return 0;  //io-&gt;FLAG1 has not changed
   }
  </code></pre>
  
  <p>It would be sufficient if different bits of the structure changed during the execution of the function, but my main criterion is that for a certain branch to be reached, the value of a volatile variable changes <strong>during</strong> the execution of the function.</p>
  
  <p>I'd be very grateful for any real-life examples. I haven't been able to find many examples using volatile on the web.</p>