Hi, Dear Coders,
I had a project: grab an image, then process the image.
Process like this:
1)In thread 1(file1.c): ... -> grab image start -> wait for grab finished flag -> process image -> ...
2)In thread 2 (file2.c): grab image, need call the camera API to grab image; when grab finished, set the grab finished flag on;
3)In file3.c: grab image API
My problem is: failed to get the grab finished flag.
Below is my code snippets:
a) file1.c: image grab & processing
DWORD dwRet=0.0;
SetEvent(st3SnapStartEv);
dwRet=WaitForSingleObject(st3SnapEndEv,1000.0);
switch(dwRet)
{
case WAIT_FAILED:
sprintf(tmp,"%s","Snapping St3 image failed");
WriteSt3ErrorLog(tmp);
break;
case WAIT_TIMEOUT:
sprintf(tmp,"%s","Snapping St3 image timeout");
WriteSt3ErrorLog(tmp);
break;
case WAIT_OBJECT_0:
break;
}
b)file2.c: image grabbing thread
DWORD WINAPI St3SnapImageThread(LPVOID arg)
{
int retV=0;
double starttime,endtime;
char tmp[128]="";
char logMsg[128]="";
DWORD dwRet;
while(1)
{
ProcessSystemEvents();
ProcessDrawEvents();
dwRet=WaitForSingleObject(st3SnapStartEv,INFINITE);
switch(dwRet)
{
case WAIT_FAILED:
EnterCriticalSection(GetLogFileCS());
sprintf(logMsg,"%s \t %s \n",TimeStr(),"wait for St3 snap start signals failed");
WriteLogFile(logMsg);
LeaveCriticalSection(GetLogFileCS());
break;
case WAIT_TIMEOUT:
break;
case WAIT_OBJECT_0:
EnterCriticalSection(GetLtControllerCS());
OpenSt3BarLight(gparam.st3cam.barlightVal);
LeaveCriticalSection(GetLtControllerCS());
Delay(0.1);
GetCurrentDateTime(&starttime);
EnterCriticalSection(GetCameraCS());
retV=SnapMyImage(ST3CAM);
LeaveCriticalSection(GetCameraCS());
while(!IsSnapOK(ST3CAM))
{
GetCurrentDateTime(&endtime);
if((endtime-starttime) > 1.0)
{
sprintf(tmp,"%s %lf, return value=%d","St3SnapImageThread:ST3 image snapping time cost=",(endtime-starttime),retV);
WriteSt3ErrorLog(tmp);
break;
}
DelayWithEventProcessing(0.005);
}
EnterCriticalSection(GetLtControllerCS());
CloseSt3BarLight();
LeaveCriticalSection(GetLtControllerCS());
SetEvent(st3SnapEndEv);
break;
}
ProcessSystemEvents();
ProcessDrawEvents();
if (GetExitThreadsStatus())
{
EnterCriticalSection(GetLogFileCS());
sprintf(logMsg,"%s \t %s \n",TimeStr(),"got exit signal in St3 image snapping thread");
WriteLogFile(logMsg);
LeaveCriticalSection(GetLogFileCS());
break;
}
}
return 0;
}
c)file3.c: using API provided by camera vendor to grab an image
int SnapMyImage(int camId)
{
if (0 == avtcam[camId])
{
if (OpenAVTCamera(camId) < 0)
{
return -1;
}
}
mbSnapOK[camId] = 0;
VmbCaptureStart( avtcam[camId] );
if(camId==0)
VmbCaptureFrameQueue( avtcam[camId] , &frames[camId], FrameDoneCallback1A );
else
VmbCaptureFrameQueue( avtcam[camId] , &frames[camId], FrameDoneCallback2A );
VmbFeatureCommandRun( avtcam[camId] , "AcquisitionStart" );
return 0;
}
In above code, need call a callback function:
FrameDoneCallback1A
;
FrameDoneCallback2A
, as shown below:
void VMB_CALL FrameDoneCallback1A( const VmbHandle_t hCamera , VmbFrame_t *pFrame )
{
if (VmbFrameStatusComplete == pFrame ->receiveStatus)
{
imaqArrayToImage(SourceImage[0], pFrame->buffer, pFrame->width, pFrame->height);
imaqResample (DestImage[0], SourceImage[0], mnwndWidth[0], mnwndHeight[0], IMAQ_ZERO_ORDER, IMAQ_NO_RECT);
DisplayMyImage(mnwnd[0], DestImage[0]);;
mbSnapOK[0] = 1;
if (avtcam[0])
{
VmbFeatureCommandRun( avtcam[0] , "AcquisitionStop" );
VmbCaptureEnd(avtcam[0]);
}
}
else
{
VmbCaptureFrameQueue( hCamera , pFrame , FrameDoneCallback1 );
}
}
As we will check snapping status in file2.c:
while(!IsSnapOK(ST3CAM))
So I provide function
IsSnapOK(int camId)
in file3.c as:
int IsSnapOK(int camNum)
{
return mbSnapOK[camNum];
}
My problem: In single thread, grab single image will be finished in 0.3s, but in multithread (10 threads in total), I will received this line each time:
St3SnapImageThread:ST3 image snapping time cost=1.0320
which means image grabbing will cost more than 1s.
I checked the image grabbed, it is correct.
If In enlong the timeout from 1.0s to 3.0s, the log file will shown as:
St3SnapImageThread:ST3 image snapping time cost=3.036
which means image grabbing will cost more than 3s.
I wonder: why I failed to get the grabbing status: mnSnapOk[0], mnSnapOk[1]?
A): Can you show me why I failed on this?
B): How can I fix this issue?
May I show my problem clearly.
Thank you very much.