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

5.7k views Asked by At

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.

4

There are 4 answers

0
P.P On BEST ANSWER

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.

0
snyggt On

The only time you need to use Locks is when data is modified on a shared resource. Eg if some threads where used to write data and some used to read data (in both cases from the same resource) then you only need a lock for when writing is done. This is to prevent whats known as "race".

There is good information of race on google for when you make programs that manipulate data on a shared resource.

11
eerorika On
  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

0
Richard Mpanga On

You are on the right track.

1) For the first array (read only) , you do not need to utilize a mutex lock for it. Since the threads are just reading not altering the data there is no way a thread can corrupt the data for another thread

2) I'm a little confused by this question. If you know that thread 1 will only write an element to array slot 1 and thread 2 will only write to array slot 2 then you do not need a mutex lock. However I'm not sure how your achieving this property. If my above statement is not correct for your situation you would definitely need a mutex lock.

3) Given the definition of atomic:

Atomic types are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads.

Key note, a mutex lock is atomic meaning that there is only 1 assembly instruction needed to grab/release a lock. If it required 2 assembly instructions to grab/release a lock, a lock would not be thread safe. For example, if thread 1 attempted to grab a lock and was switched to thread 2, thread 2 would grab the lock.

Use of atomic data types would decrease your overhead but not significantly.

4) I'm not sure how you can assure your variables are lined. Since threads can switch at any moment in your program (Your OS determines when a thread switches)

Hope this helps