从PCI上读取数据 线程和定时器效率
线程:
mythread=AfxBeginThread(StartContinuous,(LPVOID)1,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
mythread->ResumeThread();
UINT StartContinuous(LPVOID pParam)
{ HWND hWnd=(HWND) pParam;
int len=0;
FILE *fp;
i=0;
while(threadenable)
{
len = CPCI6312_ReadData(buf);
if (len>0)
{
reccnt += len;
fwrite(buf,1,len,fp);
}
}
len = CPCI6312_ReadLastData(buf);
if (len>0)
{
reccnt += len;
fwrite(buf,1,len,fp);
}
fflush(fp);
fclose(fp);
AfxMessageBox("end",MB_OK,NULL);
return 0;
}
计数器方式:
void CDemo2Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent==1)//更新记录数据
{
runsec++;
str.Format("record:%d (%d)",reccnt,runsec);
m_list.AddString(str);
}
if (nIDEvent==2)//采集数据
{
len = CPCI6312_ReadData(buf);
if (len>0)
{
reccnt += len;
fwrite(buf,1,len,fp);
}
}
CDialog::OnTimer(nIDEvent);
}
同样的数据量,线程方式居然占用CPU 80% 以上,而计数器不到30%。