初学多线程的代码,请帮忙看看,该怎么处理
初学多线程的代码,请帮忙看看
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std;
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);
int index = 0;
HANDLE hMutex;
void main()
{
HANDLE hThrd1;
HANDLE hThrd2;
hThrd1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThrd2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
CloseHandle(hThrd1);
CloseHandle(hThrd2);
hMutex = CreateMutex(NULL,FALSE,NULL);
Sleep(5000);
}
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 1 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 2 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
为什么线程不是顺序执行,而是按照1 2 2 1 2 1 2 1 1这样的顺序执行,index值也不是1--10这么输出,而是1 2 4 4 5 6 7 8 9 10输出。有谁知道吗?帮帮忙解释一下。谢谢,在线等
------解决方案--------------------
线程的执行顺序是随机的
想将index按照1-10输出要将index在线程内定义
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
int index = 0;
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 1 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
int index = 0;
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 2 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
------解决方案--------------------
就如 zottff修改的,关键是CreateMutex的调用时机:
你的代码中:
void main()
{
HANDLE hThrd1;
HANDLE hThrd2;
hThrd1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThrd2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
CloseHandle(hThrd1);
CloseHandle(hThrd2);
hMutex = CreateMutex(NULL,FALSE,NULL);//这里才调用CreateMutex,此时线程已经创建
Sleep(5000);
}
CreateMutex调用前,hMutex是无效的,WaitForSingleObject(hMutex,INFINITE)立即返回,无法产生同步的作用。
直到CreateMutex被调用,WaitForSingleObject才能阻塞
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std;
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter);
int index = 0;
HANDLE hMutex;
void main()
{
HANDLE hThrd1;
HANDLE hThrd2;
hThrd1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThrd2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
CloseHandle(hThrd1);
CloseHandle(hThrd2);
hMutex = CreateMutex(NULL,FALSE,NULL);
Sleep(5000);
}
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 1 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 2 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
为什么线程不是顺序执行,而是按照1 2 2 1 2 1 2 1 1这样的顺序执行,index值也不是1--10这么输出,而是1 2 4 4 5 6 7 8 9 10输出。有谁知道吗?帮帮忙解释一下。谢谢,在线等
------解决方案--------------------
线程的执行顺序是随机的
想将index按照1-10输出要将index在线程内定义
DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
int index = 0;
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 1 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
int index = 0;
while(index++ <10)
{
WaitForSingleObject(hMutex,INFINITE);
cout < < "thread 2 is running. index= " < <index < <endl;
ReleaseMutex(hMutex);
}
return 0;
}
------解决方案--------------------
就如 zottff修改的,关键是CreateMutex的调用时机:
你的代码中:
void main()
{
HANDLE hThrd1;
HANDLE hThrd2;
hThrd1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThrd2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
CloseHandle(hThrd1);
CloseHandle(hThrd2);
hMutex = CreateMutex(NULL,FALSE,NULL);//这里才调用CreateMutex,此时线程已经创建
Sleep(5000);
}
CreateMutex调用前,hMutex是无效的,WaitForSingleObject(hMutex,INFINITE)立即返回,无法产生同步的作用。
直到CreateMutex被调用,WaitForSingleObject才能阻塞