Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ arrays with threads - do I need to use mutexes or locks?

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.

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.

I have two fixed-size double arrays.

  • 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.

  • 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
    is written to it.

My questions then:

  1. 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?

  2. 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?

  3. Should I use atomic data types, and will there be any significant time overhead if I do?

  4. 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?

The code will be implemented on 64bit Linux. I am planning on using Boost libraries for multithreading.

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 "accepted answer," but all the answers and comments were equally helpful.

like image 206
BarbieBear Avatar asked Jun 10 '15 14:06

BarbieBear


2 Answers

  1. 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?

No. Because the data is never modified, there cannot be synchronization problem.

  1. 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?

Depends.

  1. If any other thread is going to read the element, you need synchronization.
  2. If any thread may modify the size of the vector, you need synchronization.

In any case, take care of not writing into adjacent memory locations by different threads a lot. That could destroy the performance. See "false sharing". Considering, you probably don't have a lot of cores and therefore not a lot of threads and you say write is done only once, this is probably not going to be a significant problem though.

  1. Should I use atomic data types and will there be any significant time over head if I do?

If you use locks (mutex), atomic variables are not necessary (and they do have overhead). If you need no synchronization, atomic variables are not necessary. If you need synchronization, then atomic variables can be used to avoid locks in some cases. In which cases can you use atomics instead of locks... is more complicated and beyond the scope of this question I think.

Given the description of your situation in the comments, it seems that no synchronization is required at all and therefore no atomics nor locks.

  1. ...Would my array elements in this example be aligned, or is there some way to ensure they are?

As pointed out by Arvid, you can request specific alignment using the alginas keyword which was introduced in c++11. Pre c++11, you may resort to compiler specific extensions: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Attributes.html

like image 167
eerorika Avatar answered Oct 04 '22 23:10

eerorika


Under the two conditions given, there's no need for mutexes. Remember every use of a mutex (or any synchronization construct) is a performance overhead. So you want to avoid them as much as possible (without compromising correct code, of course).

  1. No. Mutexes are not needed since threads are only reading the array.

  2. No. Since each thread only writes to a distinct memory location, no race condition is possible.

  3. No. There's no need for atomic access to objects here. In fact, using atomic objects could affect the performance negatively as it prevents the optimization possibilities such as re-ordering operations.

like image 22
P.P Avatar answered Oct 04 '22 21:10

P.P