07-09-2018 02:05 PM - edited 07-09-2018 02:10 PM
I have a list of function pointers and first I need to find a way to launch one or more of them on a single thread. Then ultimately I need to be able to launch many threads with one or more functions running on each thread. The problem is that I don't know what the function pointers are or how many there are or which ones should be launched together on any particular thread until run time. Right now I'm using CmtScheduleThreadPoolFunction to launch the threads.
Solved! Go to Solution.
07-10-2018 09:30 AM
While I don't know the answer off the top of my head, I would check out this article on Multithreading in LabWIndows/CVI, as it has some examples using CmtScheduleThreadPoolFunction. Is this helpful?
http://www.ni.com/white-paper/3663/en/
07-10-2018 01:19 PM
Yes, thank you. I've been combing through this and other articles. I understand how to launch a function on a new thread so I'm wondering what will happen if I assign a function pointer to be called inside that function and then launch it on a thread. Can I then change the function pointer in the same function and launch it again? I'm guessing not. So maybe I will have to make several empty functions that each hold a function pointer and launch them one by one. Hmmmm.
07-24-2018 02:55 PM
Ultimately I started with a sample program included in the LabWindows/CVI 2015 install. If you installed sample programs with LabWindows/CVI it can usually be found at
C:\Users\Public\Documents\National Instruments\CVI2015\samples\utility\Threading\ThreadPool\MultiPanel
Write a callback function matching the following signature
int CVICALLBACK ChartPanelMain (void * data)
{
}
You can launch as many of these functions as you want each on a new thread with the CmtScheduleThreadPoolFunction() function. Any structure can be passed in as a pointer to the "void * data" input argument. Inside the CVICALLBACK function you can cast the incoming pointer as your defined structure and everything you assigned before starting the new thread will be there. I also realized that there is no such thing as running 2 functions on a single thread unless they run successively in series. So ultimately I created a new thread for every function. If you do so, best practice dictates that you protect shared resources like open files, gui controls even indicator flags because these can all be messed up badly if 2 threads try to act on the shared resource at the same time. The simplest solution is to create a lock for each shared resource so that only one thread can access it at a time. Other options include semaphores, Mutexes and thread safe variables and queues. This article is brief but is a good overview of all the options mentioned above.