在我的数据采集程序中用了一个CList<unsigned short,unsigned short>来保存采集到的数据,每秒采集3k个unsigned short型数据,每采集到一个数据就用AddTail()加到CList中,奇怪的是采集过程中我用任务管理器监视程序的运行,数据采集进程使用的内存每秒增加100k左右,不知这是为什么?基本可以保证我的程序别的地方没有增加内存开销,因为当我把向CList中添加数据的代码注释掉以后内存使用就没有增加了。
调试欢乐多
我的程序有两个线程:一个界面线程,一个采集线程;下面是采集线程的代码:
UINT SampleThreadProc(LPVOID pParam)
{
//等待采集线程开始信号
::WaitForSingleObject(g_eventStart, INFINITE); TRACE("starting computation\n");
HANDLE hRead = INVALID_HANDLE_VALUE;
char *pinBuf = NULL;
HANDLE hDEV;
FILE *ft;
int j,k;
UINT success;
unsigned long nBytesRead;
double val;
//打开设备管道
hRead = open_file( inPipe);
pinBuf = (char *)malloc(READ_BLOCK_SIZE);
hDEV = open_dev();
//读取到的数据写入文件
ft=fopen("d:\\tt.txt","w"); //询问数据是否准备好
do
{
success = CanAcquisition(hDEV);
}while(0 == success);
//测试运行速度
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&tbegin);
QueryPerformanceCounter(&tstart); static int count=0;
if(pinBuf && hRead != INVALID_HANDLE_VALUE && hDEV)
{
SetMode( hDEV );
while(true)
{
QueryPerformanceCounter(&tstart);
if(success)
{
success = ReadFile(hRead,
pinBuf,
READ_BLOCK_SIZE, //4000
&nBytesRead,
NULL);
count++;
for (j=0; j<nBytesRead; j++)
{
unsigned short st;
unsigned char ch[2];
ch[0]=*(pinBuf+j);
j++;
ch[1]=*(pinBuf+j); st = ch[0]+ch[1] *256;
fprintf(ft,"%hu ",st);
PulseData.AddTail(st); //CList<unsigned short,unsigned short> PulseData;
//采样数据
}
}
fprintf(ft,"\n ");
QueryPerformanceCounter(&tend);
val = ((double)tend.QuadPart - (double)tstart.QuadPart)/(double)freq.QuadPart;
if (::WaitForSingleObject(g_eventKill, 0) == WAIT_OBJECT_0)
break;
}
}
QueryPerformanceCounter(&tend); CloseHandle(hDEV); val = ((double)tend.QuadPart - (double)tbegin.QuadPart)/(double)freq.QuadPart;
fprintf(ft,"Read over,Timer Ends, when time counts at %f\n",val);//timecounter);
fprintf(ft,"Read %d times \n",count);//timecounter);
fclose(ft);
return 0; // ends the thread
}
“1秒钟”是由硬件决定的,while循环不停读数据,ReadFile()从设备读数据,设备每秒能采集好3k个unsigned short型数据,设备准备好数据之后ReadFile()才返回。
PulseData我也跟踪过,没有异常情况,也就是说每秒增加约3k个数据。