新手刚学多线程,不晓得这个跟双核有关不?
#include <windows.h>
#include <iostream.h>
int index=0;
HANDLE hMutex;
DWORD WINAPI Fun1Proc(LPVOID lpParameter)
{
while(index++<1000)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<index<<endl;
cout<<"thread1 is running"<<endl;
ReleaseMutex(hMutex);
}
return 0;
}
void main()
{
HANDLE hThread1;
hThread1=CreateThread(NULL,0,Fun1Proc,NULL,0,NULL);
CloseHandle(hThread1);
hMutex=CreateMutex(NULL,FALSE,NULL);
while(index++<1000)
{
WaitForSingleObject(hMutex,INFINITE);
cout<<index<<endl;
cout<<"main thread is running"<<endl;
ReleaseMutex(hMutex);
}
}
为什么输出结果会从852开始,最后输出的数能取到1000?
而我把while(index++<1000)改成100,输出结果就是从1开始到100了?
想要输出结果从1到1000该怎么做呢
[解决办法]
从852开始是因为你的控制台缓冲区满了