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

I am new to using streams and read a lot about how data is shared and protects data. But I also didnโ€™t understand very well when I needed to use mutexes and locks to protect data.

The following is a description of the problem that I will work on. It is important to note that this will be time critical, so I need to reduce my overhead as little as possible.

I have two arrays with two sizes.

  • The first array will provide data for subsequent calculations. Themes will read values โ€‹โ€‹from it, but they will never be changed. an element can be read at some point by any of the threads.

  • The second array will be used to store the calculation results.
    performed by threads. An element of this array will only ever be updated by one thread, and probably only once, when the value of the result is written to it.

my questions then?

  • Do I need to use a mutex in a stream every time I access data from a read-only array? If you could explain why?

  • Do I need to use the mutex in the stream when writing to the array of results, although this will be the only stream that this element ever writes?

  • Should I use atomic data types and will there be any significant time on my head if I do this?

  • A lot of answers to this question seem to be - no, you donโ€™t need a mutex if your variables are aligned. Can the array elements in this example be aligned or is there some way to make sure they are?

The code will be implemented on 64-bit Linux. I plan to use Boost libraries for multithreading.

thanks for the answers and comments I pondered this and looked over the network for several days, and once posted the answer, and clear explanations returned literally in seconds. There is an โ€œaccepted answerโ€, but all answers and comments were equally helpful. Thanks again

+9
c ++ c arrays multithreading mutex
Jun 10 '15 at 14:02
source share
4 answers

In these two conditions, mutexes are not necessary. Remember that every use of a mutex (or any synchronization construct) is an overhead. Therefore, you want to avoid them as much as possible (without prejudice to the correct code, of course).

  • No. Mutexes are not needed, as threads only read the array.

  • No. Since each stream is recorded only in a separate memory cell, no race condition is possible.

  • No. There is no need for atomic access to objects. In fact, the use of atomic objects can adversely affect performance, as this prevents optimization capabilities such as reordering operations.

+3
Jun 10 '15 at 14:28
source share
  • Do I need to use a mutex in a stream every time I access data from a read-only array? If you could explain why?

No. Since the data never changes, the synchronization problem cannot be.

  1. Do I need to use the mutex in the stream when writing to the array of results, although this will be the only stream that this element ever writes?

It depends.

  • If any other thread will read the item, you will need synchronization.
  • If any thread can resize the vector, you will need synchronization.

In any case, take care not to write to adjacent memory cells with different threads. This can destroy performance. See "False sharing." Given that you probably do not have many cores and therefore not many threads, and you say that recording is performed only once, this is probably not a serious problem.

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

If you use mutex locks, atomic variables are not needed (and they have overhead). If you do not need synchronization, atomic variables are not needed. If you need synchronization, you can use atomic variables in some cases to prevent locks. In which cases you can use atoms instead of locks ... 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, does not atomize or block.

  1. ... Can the array elements in this example be aligned, or is there a way to make sure they are?

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

+7
Jun 10 '15 at 14:13
source share

The only time you need to use Locks is when data changes on a shared resource. For example, if some streams are used to write data, and some are used to read data (in both cases from the same resource), you only need to lock when writing a record. This should prevent what is called a race.

There is good google racing information when you make programs that manage data on a shared resource.

+1
Jun 10 '15 at 14:34
source share

You are on the right track.

1) For the first array (read-only) you do not need to use a mutex lock for it. Since threads just read without changing data, in no way can a thread ruin the data for another thread

2) I am a little confused by this question. If you know that stream 1 will only write an element to cell 1 of the array, and stream 2 will only write to cell 2 of the array, then you do not need a mutex lock. However, I am not sure how you achieve this property. If my above statement is not suitable for your situation, you will definitely need a mutex lock.

3) Given the definition of an atom:

Atomic types are types that encapsulate a value, access to which is not guaranteed to lead to data failures and can be used to synchronize memory access between different threads.

Key note: a mutex lock is an atomic value that only one assembly instruction is required to lock / unlock the lock. If 2 assembly instructions were required to lock / unlock, the lock would not be flow safe. For example, if thread 1 tried to capture a lock and was switched to thread 2, thread 2 would capture a lock.

Using atomic data types will reduce your overhead, but not significantly.

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

Hope this helps

0
Jun 10 '15 at 2:31 on
source share



All Articles