Can two threads write to another element of the same array?

I was wondering if two threads can modify elements of the same array.

If I have an unsigned char array[4] , can thread1 set array[0] and array[1] to 'A' and thread2 can set array[2] and array[3] to 'B' at the same time without problems?

+5
source share
3 answers

By definition, a race condition occurs when 1 or more threads write data to the same place in memory, while others read from it (or write to it too). Will several streams, each of which modifies another element of the array, be written to the same place in memory? The answer is no. Each element of the array has a memory area reserved for it only inside the area attributed to the entire array. Therefore, modifications of various elements are not recorded in any of these places.

In fact, I asked this question a very long time here , and on its basis a part of my doctoral work was included. I connected hundreds of curves (least squares finalization) in parallel, while updating a single array that has results in multiple threads.

+2
source

In all the systems I came across, it is not a functional problem when different streams are written to different elements of the same array. On some systems, this may be a performance issue due to threads on different cores that access data in the same cache line. HW will solve the functional part, but performance may be poor.

A functional problem does not start until you want one thread to read data written by another thread. At that time, you will need a mechanism (e.g. mutexes, semaphore, atomic operations, etc.) to ensure that the recorded data will be visible to all other streams.

+1
source

If the threads are running on the same core, this will be a sequential write process in which you will set different bytes in the same way that the "linear" code will be on the same thread. However, you do not necessarily know in what order (in the usual case). The value of what the thread executes in what order, when.

However, if the records come from two different cores, the cache line will be marked with the corresponding kernel cache and the write operation will be declared using the RFO message. This will affect performance.

So, in other words, continue to write to the same cache line in the same kernel as you can.

More information can be found here β†’ What every programmer should know about memory

/ Anders

+1
source

Source: https://habr.com/ru/post/1270934/


All Articles