# Find memory conflicts among multiple threads in C++

Suppose we have a RAM and that RAM is organized in blocks. there are multiple processes running on the system. We have to keep in mind that every process gets following information, (Thread T, Memory Block M, time t, R/W) This indicates the thread T was implementing memory block M at given time t and operation could be either read(R) or write(W).

The following case is indicating whether it is memory conflict or not −

• More than one read operations at the same location are not the reason of conflict.

• When writing operation is being performed between x+5 to x-5 to location of M, it will be responsible for creating a conflict for a thread accessing location M at time x where x is termed as some time.

So, if thread T1 accessed memory location M at time x+1 and thread T2 accesses location M before the time x+6, then T1 and T2 are making conflict when one of them does write operation.

If we have a list of threads accessing memory locations. We have to find all conflicts.

So, if the input is like [(1, 932, 1, R), (2, 512, 2, W), (3, 932, 3, R), (4, 512, 4, R), (5, 432, 5, R), (6, 512, 6, R),(7, 835, 7, W), (8, 432, 8, R)], then the output will be Conflicting threads (2, 4) and (2, 6), And all other operations are same.

To solve this, we will follow these steps −

• Create Thread with id, memory_block, time and operation

• sort the array th_arr based on memory block, when memory blocks are same then using time.

• for initialize i := 1, when i − n, update (increase i by 1), do −

• if th_arr[i].memory_block is same as th_arr[i - 1].memory_block, then −

• if th_arr[i].time <= th_arr[i-1].time+5, then −

• j := i - 1

• while (th_arr[i].memory_block is same as th_arr[j].memory_block and th_arr[i].time <= th_arr[j].time+5 and j >= 0), do −

• if th_arr[i].operation is same as 'W' or th_arr[j].operation is same as 'W', then −

• display conflicting threads th_arr[j] and th_arr[i]

• (decrease j by 1)

## Example (C++)

Let us see the following implementation to get better understanding −

Live Demo

#include<bits/stdc++.h>
using namespace std;
public:
int id, memory_block, time;
char operation;
};
if (x.memory_block == y.memory_block)
return x.time < y.time;
else return x.memory_block < y.memory_block;
}
void display_conflicts(Thread th_arr[], int n) {
sort(th_arr, th_arr+n, compare);
for (int i = 1; i < n; i++) {
if(th_arr[i].memory_block == th_arr[i-1].memory_block) {
if (th_arr[i].time <= th_arr[i-1].time+5) {
int j = i-1;
while (th_arr[i].memory_block == th_arr[j].memory_block && th_arr[i].time <= th_arr[j].time+5 && j >= 0) {
if (th_arr[i].operation == 'W' || th_arr[j].operation == 'W') {
cout << "Conflicting threads [" << th_arr[j].id << ", " << th_arr[i].id << "]\n";
}
j--;
}
}
}
}
}
int main() {
Thread th_arr[] = {{1, 932, 1, 'R'},{2, 512, 2, 'W'},{3, 932, 3, 'R'}, {4, 512, 4, 'R'},{5, 432, 5, 'R'}, {6, 512, 6, 'R'},{7, 835, 7, 'W'}, {8, 432, 8, 'R'}};
int n = sizeof(th_arr)/sizeof(th_arr);
display_conflicts(th_arr, n);
}

## Input

{{1, 932, 1, 'R'},{2, 512, 2, 'W'},{3, 932, 3, 'R'}, {4, 512, 4,
'R'},{5, 432, 5, 'R'}, {6, 512, 6, 'R'},{7, 835, 7, 'W'}, {8, 432, 8,
'R'}}

## Output

Conflicting threads [2, 4]
Conflicting threads [2, 6]