我创建三十个线程如下Bool Loop=true;
......
for (i=0;i<=30;i++)
{
AfxBeginThread((AFX_THREADPROC)ChnExter,this);
}
......UINT ChnExter(LPVOID pParam)
{
CTwVoiceDlg *ptwVoicedlg=(CTwVoiceDlg *)pParam;
Bool Test=true
while(Loop==true)
{
if (test==true)
{
.......
test=false;
}
else
{
.......
test=true; }
Sleep(100);
}
AfxEndThread(0);
return 0;
}这样写后好象调试是对的,线程好象是创建了30个,但是从1到29个都只执行了一次,执行到test==true后的语句就停止了,好象线程就被挂起了似的,只有最后的一个线程会在那个While里面循环,请教各位这是为什么?怎么解决?
......
for (i=0;i<=30;i++)
{
AfxBeginThread((AFX_THREADPROC)ChnExter,this);
}
......UINT ChnExter(LPVOID pParam)
{
CTwVoiceDlg *ptwVoicedlg=(CTwVoiceDlg *)pParam;
Bool Test=true
while(Loop==true)
{
if (test==true)
{
.......
test=false;
}
else
{
.......
test=true; }
Sleep(100);
}
AfxEndThread(0);
return 0;
}这样写后好象调试是对的,线程好象是创建了30个,但是从1到29个都只执行了一次,执行到test==true后的语句就停止了,好象线程就被挂起了似的,只有最后的一个线程会在那个While里面循环,请教各位这是为什么?怎么解决?
你的Loop最好一个线程一个,在参数中传入,还有在线程内部调用AfxEndThread(0)干嘛。
#include <windows.h>DWORD myThread(DWORD param);main()
{
HANDLE h=NULL;
int i;
int TID;
for(i=0;i<30;i++)
{
h=CreateThread(NULL,0,(void *)&myThread,i,0,&TID);
if (h) CloseHandle(h);
}
_getch();
printf("press any key to end.\n");
}
DWORD myThread(DWORD param)
{
while(1)
{
printf("%d.\n",param);
Sleep(1000);
}
return 0;
}
起码你的每条话路的序号和板卡号就应该不同,你可以像这样来启动你的30条线程
for (int nBoard = 0; nBoard < BOARD_NO; nBoard ++){
for (int nTrunk = 0; nTrunk < MAX_LINE; nTrunk ++){
CString temp; temp.Format("%d %d", nBoard, nTrunk);
AfxBeginThread(LineControl, &temp, NULL);
Sleep(1000);
}
}
至于线程函数就是每个话路的功能了,没有什么特殊!
CTwVoiceDlg *ptwVoicedlg=(CTwVoiceDlg *)pParam;
可以取到所需要的参数
http://expert.csdn.net/Expert/topic/2634/2634229.xml?temp=.8815119