I have a global shared variable and that is being updated 5 times by each of the 5 threads spawned. As per my understanding the increment operation is consisting of 3 instructions
load reg, M
inc reg
store reg, M
So I want to ask that in this scenario what would be the maximum and minimum value given arbitrary interleaving in the 5 threads.
So according to me the maximum value will be 25 ( I am 100% sure that it can be more than 25) and the minimum value is 5. But I am not so sure on minimum value. Can it be less that 5 in some arbitrary interleaving ? Any inputs will be greatly appreciated.
/* Global Variable */
int var = 0;
/* Thread function */
void thread_func()
{
for(int c = 0; c < 5; c++)
var++;
}
Given your definition of increment, I agree with your max of 25.
However, I believe the min can be 2 under the following scenario. I've named the 5 threads A, B, C, D and E.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With