Is synchronization required when using two threads to operate on different array indices in C/C++?


Creator

Suppose I have an array defined as follows:

volatile char v[2];

I have two threads (represented by A, B respectively) manipulating an array v. If I make sure that A, B use different indices at all times, that is, if A is operating now v[i], then B is either doing nothing or operating v[1-i]. I wonder if synchronization is required for this situation?

I've mentioned this question , but I think this is limited in Java. I'm asking this because I've been struggling with a large project for days, and I've been struggling with it until now, and the only reason I can explain this error is that synchronization requires the above manipulation . (Since the bug is so rare, it's hard for me to prove my guess is correct)

Edit: Can be read and modified v.

Sam

This could be a compiler bug or a hardware limitation.

Sometimes when less than 32bit/64bit variables are accessed from memory, the processor will read 32bits, set the appropriate 8bit or 16bit, and write back the entire register. This means it will read/write adjacent memory at the same time, causing a data race.

The solution is

  • Use byte access instructions. They may not be available for your processor, or your compiler doesn't know how to use them.

  • Pad your elements to avoid this sharing. If the target platform does not support byte access, the compiler should do this automatically. But in arrays this conflicts with memory layout requirements.

  • Synchronize the entire structure

C++03/C++11 debate

In classic C++ it is your responsibility to avoid/mitigate this behavior. In C++11, this violates the requirements of the memory model, as described in other answers.

Related


When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

When using synchronization, atomic references are not required

Maxim Dmitriev The code is from the Java Concurrency Guidelines by Fred Long . I understand that a set of atomic operations is not atomic. Therefore, the following code does not meet the requirements. To find the code, see page 23. public class Adder { pr

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

With ConcurrentHashMap, when is synchronization required?

For each position: I have a ConcurrentHashMap where I do the following: sequences = new ConcurrentHashMap<Class<?>, AtomicLong>(); if(!sequences.containsKey(table)) { synchronized (sequences) { if(!sequences.containsKey(table)) initial

Operate on two arrays with different numblocks

R to I am trying to implement take_along_axis for dask arrays . What is the standard way of mapping operations that take a block from a dask array and Athe corresponding block of the dask array B? When should rechunk be used A.numblocks != B.numblocks? Mecoxla

Operate on two arrays with different numblocks

R to I am trying to implement take_along_axis for dask arrays . What is the standard way of mapping operations that take a block from a dask array and Athe corresponding block of the dask array B? When should rechunk be used A.numblocks != B.numblocks? Mecoxla

Operate on two arrays with different numblocks

R to I am trying to implement take_along_axis for dask arrays . What is the standard way of mapping operations that take a block from a dask array and Athe corresponding block of the dask array B? When should rechunk be used A.numblocks != B.numblocks? Mecoxla

Operate on two arrays with different numblocks

R to I am trying to implement take_along_axis for dask arrays . What is the standard way of mapping operations that take a block from a dask array and Athe corresponding block of the dask array B? When should rechunk be used A.numblocks != B.numblocks? Mecoxla

Too many indices for array when using matplotlib

Sathya Thanks for taking the time to read this question. I am trying to draw a pie chart in one row. The number of pie charts depends on the results returned. import matplotlib.pyplot as plt import numpy as np fig, axs = plt.subplots(1,len(to_plot_arr)) label

Only two threads are allowed to operate on a function

Dheeraj Joshi I have an unusual question. I have a function where the operations in this function can be done by two threads at a time. static int iCount = 1; public synchronized void myFunct(){ while(iCount >= 3) { try { wait(); } catch (Interru

Only two threads are allowed to operate on a function

Dheeraj Joshi I have an unusual question. I have a function where the operations in this function can be done by two threads at a time. static int iCount = 1; public synchronized void myFunct(){ while(iCount >= 3) { try { wait(); } catch (Interru

Only two threads are allowed to operate on a function

Dheeraj Joshi I have an unusual question. I have a function where the operations in this function can be done by two threads at a time. static int iCount = 1; public synchronized void myFunct(){ while(iCount >= 3) { try { wait(); } catch (Interru

How to compare two array indices and get different strings?

Rahul Narhe I have two arrays. I want to compare these arrays and find their distinct strings. predecessor. $a = array('a','c','b','e','x'); $b = array('a','b','c','d','e','f'); I want strings that are not common in both arrays. MLeFevre array_diff() Doing th

How to compare two array indices and get different strings?

Rahul Narhe I have two arrays. I want to compare these arrays and find their distinct strings. predecessor. $a = array('a','c','b','e','x'); $b = array('a','b','c','d','e','f'); I want strings that are not common in both arrays. MLeFevre array_diff() Doing th

assign value when two df have different indices

Thomas I have two dfs that look like this: Symbol Sector Sub-industry Company Weight SectorSymbol Ticker MMM Industrials Conglomerates MCompany 0.602676 XLI ABT Health Care Equipment Abbott Lab 0.401900 XLV A

assign value when two df have different indices

Thomas I have two dfs that look like this: Symbol Sector Sub-industry Company Weight SectorSymbol Ticker MMM Industrials Conglomerates MCompany 0.602676 XLI ABT Health Care Equipment Abbott Lab 0.401900 XLV A

assign value when two df have different indices

Thomas I have two dfs that look like this: Symbol Sector Sub-industry Company Weight SectorSymbol Ticker MMM Industrials Conglomerates MCompany 0.602676 XLI ABT Health Care Equipment Abbott Lab 0.401900 XLV A