How to use locks to prevent a race condition
<p>I have a bug in my code which I have identified to be a race condition. I know it is a race condition, because it occurs intermittently. I have researched how to prevent these race conditions and I came across this</p>

<pre><code>      for ( int i = 0; i &lt; 10000000; i++ )
      {
           //lock x
           x = x + 1; 
           //unlock x
      }
</code></pre>

<p>Could somebody please go into further detail on how I may implements these locks?</p>