关于 volatile ,进来看看
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <process.h>
volatile int Value0 = 0;
unsigned int _stdcall ThdFun0(void* param)
{
int Count = 0;
while(1)
{
++Count;
++Value0;
if (Value0 > 1e8)
{
break;
}
}
printf("Thd End Count = %d\n", Count);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hThread0 = (HANDLE)_beginthreadex(NULL, 0, ThdFun0, NULL, 0, NULL);
HANDLE hThread1 = (HANDLE)_beginthreadex(NULL, 0, ThdFun0, NULL, 0, NULL);
WaitForSingleObject(hThread0, INFINITE);
WaitForSingleObject(hThread1, INFINITE);
char ch = getchar();
return 0;
}
如上代码,为什么两次输出值之和远大于1e8,接近2e8