title: C/C++ arrays with threads - do I need to use mutexes or locks?
body: <p>I am new to using threads and have read a lot about how data is shared and protected.  But I have also not really got a good grasp of using mutexes and locks to protect data.</p>
  <p>Below is a description of the problem I will be working on.  The important thing to note is that it will be time-critical, so I need to reduce overheads as much as possible.</p>
  <p>I have two fixed-size double arrays.</p>
  <ul>
  <li><p>The first array will provide data for subsequent calculations.
  Threads will read values from it, but it will never be modified. An element may be read at some time by any of the threads.</p>
  </li>
  <li><p>The second array will be used to store the results of the calculations performed by the threads.  An element of this array will only ever be updated by one thread, and probably only once when the result value<br />
  is written to it.</p>
  </li>
  </ul>
  <p>My questions then:</p>
  <ol>
  <li><p>Do I really need to use a mutex in a thread each time I access the data from the read-only array?  If so, could you explain why?</p>
  </li>
  <li><p>Do I need to use a mutex in a thread when it writes to the result array even though this will be the only thread that ever writes to this element?</p>
  </li>
  <li><p>Should I use atomic data types, and will there be any significant time overhead if I do?</p>
  </li>
  <li><p>Many answers to this type of question seem to be - no, you don't need the mutex if your variables are aligned.  Would my array elements in this example be aligned, or is there some way to ensure they are?</p>
  </li>
  </ol>
  <p>The code will be implemented on 64bit Linux.  I am planning on using Boost libraries for multithreading.</p>
  <p>I have been mulling this over and looking all over the web for days, and once posted, the answer and clear explanations came back in literally seconds. There is an &quot;accepted answer,&quot; but all the answers and comments were equally helpful.</p>