When different PARFOR loops process this line:
results(1,1) = results(1,1) + 1;
The following can happen:
- Thread 1: results(1,1) is evaluated and stored temporarily
- Thread 2: 1 is added to the temporary variable
- Thread 2: results(1,1) is evaluated and stored temporarily
- Thread 1: results(1,1) is updated
- Thread 2: results(1,1) is updated
Now the results(1,1) is increased by 1, and not by 2. This happens because there is no mutex to block simultaneous access to result.
If the increment is atomic, there would be no need to implement InterlockedIncrement(). While the 32 bit increment is usually atomic on modern compilers and processors, this is not guaranteed for 64 bit types as int64 and doubles. Using a critical section is recommended, or InterlockedIncrement().