I have given the following 2 snippets:
snippet(1):
int i=0;
while(i < n - 1){
A[i] += A[i+1] + f(i);
i++;
}
snippet(2):
for(inti= 0;i < n; i++){
for(int j = 0; j < n; j++)
A[i] += g(j,i);
}
so an array A of length n is defined and filled with some values assuming that there exists two functions f(.) and g(.,.) where f(.) is a thread safe function, and g(.,.) is not a thread safe function.
Every call of function g(.,.) access a memory location determined by its first
argument (i.e. g(i,j) and g(i,k) access the same memory location while the calls g(i,j)
and g(m,j) are thread safe for all 𝑖≠𝑚)
what is required is to rewrite each one of these snippets to parallelize it using threads, such that each program has 4 threads (changing the program as long as the values in the array A at the end of the threaded execution are equal to that of the serial execution)
so I need help to solve this problem or sources because I do not know how to solve such problems using threads and I cannot understand the idea fully.
What I have tried:
I searched around about threads and ways to rewrite these kind of code but without a solid answer.