LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Invoking CmtScheduleThreadPoolFunction() in a callback function.

Hi all,

I would like to know if it's ok to invoke the CmtScheduleThreadPoolFunction(...) function within a callback function instead of invoking it from main() (as shown in the examples).

The reason I'm asking is that every time I do a CmtScheduleThreadPoolFunction() and then try to "kill" my thread, my application does not behave properly.

Thank you in advance,

JP
0 Kudos
Message 1 of 5
(4,384 Views)
You can call CmtScheduleThreadPoolFunction from anywhere you want in your code. You really shouldn't kill threads, by the way. It's always risky, since you don't know what that thread is doing. What you should do is design the thread function to check a termination flag and set that termination flag to true from outside the thread. That ensures that the thread finishes cleanly. Killing threads from other threads is usually only done is extreme circumstances such as threads that have stopped responding or system shutdowns.

Best Regards,

Chris Matthews
National Instruments
Message 2 of 5
(4,384 Views)
Thank you for your quick response.

But, would this be the right way of killing my thread from the callback function that created it in the first place:

CmtGetThreadPoolFunctionAttribute(Pool_Handle, threadFunctionID, ATTR_TP_FUNCTION_THREAD_ID, &threadID);

CmtTerminateThreadPoolThread (Pool_Handle, threadID, 0);

CmtDiscardThreadPool (Pool_Handle);

QuitUserInterface(0);

?

Thanks,

JP
0 Kudos
Message 3 of 5
(4,384 Views)
No. You should hardly ever use CmtTerminateThreadPoolThread, only in extreme situations as I mentioned. As it says on the function panel for that function "Use this function with extreme caution." You should design your thread function to shutdown on its own when triggerred by the main function. It doesn't matter where in your program you created your thread. See our thread example programs to see how we do this. Basically you code would be something like this.

static int stopThread = 0;

int CVICALLBACK CallbackFunction(...)
{
//This would start the thread
CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, ThreadFunction1, NULL, &threadFunctionId1);
.
.
.
}

int CVICALLBACK ThreadFunction1 (void *fu
nctionData)
{
//Thread runs until global flag stopThread is set
while(!stopThread)
{
//Insert Code here
}
}

Then, when you want the thread to stop you just set stopThread = 1, and the thread will stop it's loop. Calling CmtTerminateThreadPoolThread kills the thread regardless of what state it is in or what it is doing. This can cause System Locks or other problems if done at the wrong time (as it warns you in the function panel for the function).

For more help with Multithreaded programming in CVI, consult the examples (under cvi\samples\utility\Threading) and the PDF file under cvi\bin\MultithreadingOverview.pdf.

Chris
0 Kudos
Message 4 of 5
(4,384 Views)
Ok, thank you. I actually really wanted to kill the thread, but since you say it might cause the system to be unstable (might react differently everytime). I'll try to use a similar solution to the one you suggested.

Regards,

JP
0 Kudos
Message 5 of 5
(4,384 Views)