LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

When does CmtScheduleThreadPoolFunction's function stop?

Im trying to invoke a new thread function from a callback function from a button on the .uir.

My code looks something like this:

int CVICALLBACK BUTTON_CALLBACK (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
CmtNewThreadPool (4, &poolHandle);
CmtScheduleThreadPoolFunction (poolHandle, thread_function, NULL, NULL);
}

thread_function(void * functionData) {

do stuff

}

My question is why does it do the stuff twice?  Won't it do the statements in the thread_function and then leave.  I only want to do stuff x 1.  Also, do i need to take any precautions if the user presses the button again?  Will it use the same thread that it used before or will it create a new thread to do the same thread_function.  Or does this thread get created & recreated since i passed the NULL parameter for the ID.  (I looked for this info on the message board and documentation but couldnt find what i was looking for)  Thanks

0 Kudos
Message 1 of 2
(3,052 Views)
Hi,

Your first problem (that it calls the function twice) is probably due to not putting a switch statement for the event type in your button callback.  The way it's written, the function will be called each time the button generates ANY event, including EVENT_GOT_FOCUS, etc, which is not what you want.  You should only schedule the thread pool function if event == EVENT_COMMIT.

Secondly, you should not create a new thread pool each time you want to run the thread function.  You could move the call to CmtNewThreadPool out of BUTTON_CALLBACK and into main, or some other initialization function.  Or better yet, you could just use the default thread pool (pass DEFAULT_THREAD_POOL_HANDLE as the handle) for scheduling your function.  When you schedule a thread pool function, it does not actually create a new thread.  It simply queues up your function to be run by the next free thread in the thread pool you specified.  In typical use cases, there will usually be at least one free thread in the pool waiting, so your function executes immediately.  When the function exits, the thread that ran it goes back to a ready state.

If you right-click on the function panel for the CmtScheduleThreadPoolFunction, you can find a more detailed explanation of what I just said.

Hope this helps clear things up.

Mert A.
National Instruments
0 Kudos
Message 2 of 2
(3,043 Views)