#include
#include
CRITICAL_SECTION cs;
static int i;
DWORD WINAPI kbinput(LPVOID pM) //把函数头换成void
{
int j=0;
for(j=0;j<20;j++){
EnterCriticalSection(&cs);
printf("%d\n",i);
i++;
LeaveCriticalSection(&cs);
}
}
DWORD WINAPI kbinput1(LPVOID pM)
{
int j=0;
for(j=0;j<20;j++){
EnterCriticalSection(&cs);
printf("%d\n",i);
i++;
i++;
LeaveCriticalSection(&cs);
}
}
int main()
{
i=0;
InitializeCriticalSection(&cs);
HANDLE handle1 = CreateThread(NULL, 0, kbinput,NULL , 0, NULL);
HANDLE handle2 = CreateThread(NULL, 0, kbinput1,NULL, 0, NULL);
Sleep(1000);
}
上面是我写的一个程序,他的互斥锁不能正常运行,两个线程完全是乱来,但是我在创建线程是把CreateThread换成_beginthread,把函数头换成void就可以了,请问这是为什么?