首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

新手刚学多线程,不晓得这个跟双核有关不?解决思路

2012-04-02 
新手刚学多线程,不晓得这个跟双核有关不?#include windows.h#include iostream.hint index0HANDLE h

新手刚学多线程,不晓得这个跟双核有关不?
#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开始是因为你的控制台缓冲区满了

热点排行