The minimum and maximum possible value of a common variable when increased by several threads

I have a global shared variable that is updated 5 times by each of the 5 threads generated. In my opinion, the increment operation consists of 3 instructions

load  reg, M
inc reg
store reg, M

So, I want to ask that in this scenario there would be a maximum and minimum value for random rotation in 5 threads.

Thus, in my maximum value there will be 25 (I am 100% sure that it can be more than 25), and the minimum value is 5. But I am not sure about the minimum value. Could it be less than 5 with some arbitrary alternation? Any input would be much appreciated.

/* Global Variable */
int var = 0;

/* Thread  function */
void thread_func()
{
     for(int c = 0; c < 5; c++)
             var++;
}
+5
source share
3 answers

, 25.

, , min 2 . 5 A, B, C, D E.

  • A 0
  • C, D, E .
  • B 4 5 .
  • 0 1 (1).
  • B 1
  • A
  • B 1 2 2.
+13

, jtdubs, 1 .

5 , A, B, C, D E.

  • A 0
  • B, C, D, E 20 (5 4 ).
  • 0 1 1.
0

2 ( 1).

1 , A , 1 . "", A 4 , 5.

, 2 , - A B , , . B "" 4 , 1, 2 A.

0

All Articles