title: lock free programming - c++ atomic
body: <p>I am trying to develop the following lock free code (c++11):</p>
  
  <pre><code>int val_max;
  std::array&lt;std::atomic&lt;int&gt;, 255&gt; vector;
  
      if (vector[i] &gt; val_max) {
      val_max =vector[i];
      }
  </code></pre>
  
  <p>The problem is that when there are many threads (128 threads), the result is not correct, because if for example val_max=1, and three threads ( with vector[i] = 5, 15 and 20) will execute the code in the next line, and it will be a data race..</p>
  
  <p>So, I don't know the best way to solve the problem with the available functions in c++11 (I cannot use mutex, locks), protecting the whole code.</p>
  
  <p>Any suggestions? Thanks in advance!</p>