I have 2 unsynchronized threads that modify the global variable. Each thread has 12 iterations. Before the modification command, each stream has a 1 ms delay. As a result, the global variable is equal to 112, which is logical, since the streams modify it simultaneously, BUT if you remove the delay in both streams, then one of them is ahead of the other stream so that the other stream receives the current value of the global variable. Why does this happen when delays are enabled?
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <omp.h>
using namespace std;
int global = 100;
HANDLE ht1, ht2;
DWORD WINAPI ThreadProc1(LPVOID lpParameter ) {
int i, j;
for (j=1; j <= 12; j++) {
i = global;
i++;
Sleep (1); //Delay before modifying global variable
global = i;
printf_s( "%4s %4d \n", " 1 th", i );
}
return 0;
}
DWORD WINAPI ThreadProc2 (LPVOID lpParameter) {
int i, j;
for (j=1; j <= 12; j++) {
i = global;
i++;
Sleep (1); //Delay before modifying global variable
global = i;
printf_s( "%4s %4d %4d \n", " 2 th", i, j );
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[]) {
HANDLE msh[2];
ht1 = CreateThread(NULL, 0, &ThreadProc1, NULL, 0, NULL);
ht2 = CreateThread(NULL, 0, &ThreadProc2, NULL, 0, NULL);
msh[0] = ht1;
msh[1] = ht2;
WaitForMultipleObjects(2,msh,TRUE, INFINITE);
return 0;
}
delayed result https://i.sstatic.net/MYJOm.png
result without delay https://i.sstatic.net/klMGc.png
Based on the thread scheduling of your operating system, when adding a delay, the two threads all execute i = global
, the global value is the same in two thread. So it's just an increase of 12. When there is no delay, the code:
i = global;
i++;
global = i;
has not been interrupted, equivalent to global++
here. So the result is 100 + 12*2 = 124.