LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Exit from a thread in a loop

 

                Hello,

                I want to exit from a thread in a 'for' loop with 1024 repetitions. For example I want to exit in 500th  repetition from the thread. I used some functions related with thread.  My code is the following:

 

static int CVICALLBACK GraphLoop (void *functionData)     // This is my thread function.
{
FILE *fp;

fp=fopen(fileName,"a");
fprintf(fp,"\n\n Input Number Amplitude Upper Level Lower Level Status\n\n");
for(i=0;i<1024;i++)
{
hpe363xa_queryOutputVolt3631 (vi, 1, &sonuc);
lowerlevel[i]=-3;
upperlevel[i]=3;

amplitude[i] = sonuc;
if (amplitude[i] > upperlevel[i] || amplitude[i] < lowerlevel[i])
{
status[i]='F';
}
else
{
status[i]='P';
}

fprintf(fp," %d %9.5f %9.5f %9.5f %c\n",inputNumber[i], amplitude[i], upperlevel[i], lowerlevel[i], status[i]);

inputNumber[i]=i+1;

plot_handle = PlotXY (testler, TESTLER_GRAPH, inputNumber, amplitude, i+1, VAL_INTEGER, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_DOT, 1, VAL_RED);

}
fclose(fp);
return 0;
}


int CVICALLBACK CBBasla (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:

if (k==1)
{
k=0;
SetCtrlAttribute (testler, TESTLER_BASLA, ATTR_DISABLE_PANEL_THEME, 1);
CmtNewThreadPool (8, &poolHandle);
CmtScheduleThreadPoolFunction (poolHandle, GraphLoop, NULL, &GraphLoopId);
//CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, GraphLoopId, OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
//CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, GraphLoopId);
SetCtrlAttribute (testler, TESTLER_BASLA, ATTR_DISABLE_PANEL_THEME, 0);
k=1;
}
else
{
};

break;
case EVENT_RIGHT_CLICK:

break;
}
return 0;
}

 

int CVICALLBACK CBTestleriKapat (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:

CmtWaitForThreadPoolFunctionCompletion (poolHandle,GraphLoopId, 0);
CmtReleaseThreadPoolFunctionID (poolHandle,GraphLoopId);
GraphLoopId = 0;

DiscardPanel(testler);
break;
case EVENT_RIGHT_CLICK:

break;
}
return 0;
}

 

 

             In this code, what can I use to exit the thread?

0 Kudos
Message 1 of 2
(4,108 Views)

It's not clear to me:

  • Do you want to tailor your function passing the number of iterations to it before it is called? You can pass the number of iterations in functionData' parameter
  • Do you want to prematurely terminate the loop while it is running? Use a global volatile variable set outside the thread; break out of the loop if you see the variable set. See here


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(4,099 Views)