LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Launching multiple function pointers on a single thread

Solved!
Go to solution

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.

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

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/

Francine P.
Applications Engineering
National Instruments
0 Kudos
Message 2 of 4
(2,451 Views)

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.

0 Kudos
Message 3 of 4
(2,446 Views)
Solution
Accepted by topic author skinnedknuckles

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.

Message 4 of 4
(2,391 Views)