01-06-2000 08:35 PM
01-06-2000 11:46 PM
01-08-2000 09:50 PM
01-09-2000 09:01 AM
05-20-2021 04:05 PM
I am also just getting started with multi-threading in LabView, and have read whatever material from NI that I can find about the topic. Can someone give some clarity about how multithreading is implemented in code? There are two separate VIs that I would like to run in parallel as different threads.
Here is what I implemented:
- created subVIs which are separate VIs that run perfectly
- placed both subVIs in a "master" VI which calls both
My questions are:
- is this how multi-threading is implemented?
- how can I prove that the threads are indeed on separate threads?
- how can I determine which thread is being executed at any given time?
- is the While Loop necessary? (only included it to create a form of partition. Would it still be multi-threading if the loops were replaced with frame structures instead, or perhaps nothing at all?)
05-20-2021 04:43 PM
LabVIEW is always executing things in parallel. The way it operates is that the compiler breaks things down to nodes which can execute. When a node is ready to run, it will be exeuted. For example, in the code you posted the compiler will consider each while loop a node. It will further break things down and start to identify the nodes which are inside each while loop. In your case, this is each of your subVIs. It will then analyze everything inside the subVIs and so on until it reachs the most primative nodes such as the add or subtract functions. A node is capable of running if all of its inputs are available. Outputs from a node do not exit the node until the node has fully excuted. So, a subVI can execute once all of its inputs are available. This is how multiple things can run in parallel within LabVIEW. It is also the foundations of dataflow programming. The wires that connect your code will dictate the execution order. If two nodes do not have any data dependency between them they both can execute in parallel. Therefore in your code example the two while loops will execute in separate threads in parallel. LabVIEW is also smart enough to execute different sectionsof code on different processors giving you better overall performance. This is one of the most excellent things about LabVIEW is that it takes care of executing things in parallel and in multiple threads. Unlike most traditional text based languages you don't have to concern yourself with creating threads and processes. The language handles that for you.
That doesn't mean however that you don't have to thinnk about your code and how it is written. You need to consider race conditions because the code does run in parallel and in separate threads. This is why it is advised that you don't use local/global variables. Use the wires to transfer data. Or when you get more experienced you can use queues, notifiers or other more advanced features.
In terms of how to know what will execute when, with the exception of the dataflow there is no gaurantee of the order of execution. Therefore, if you have nodes which do not have a data dependency between you should not expect a specific order of execution. It is best to assume they will execute at the same time. So if you need to enforce order of execution use the dataflow. Avoid using sequence structures.
With respect to your question about whether the while loops are necessary, without knowing what is in the subVIs it is impossible to say. Generally though your code will have some loops so it can execute continuously. Where those loops are depends on the architecture you choose. In most cases the top level VI (the "main" of your application) will have at least one loop. Large applications can have multiple concurrent state machines running so there will be lots of loops. You just won't see them all on the block diagram. At least not if it is a well written application. Initalization code will spawn the various state machines that are required or they can even be started dynamically during execution. They may run for the duration of the application or only brief moments to accomplish a specific task and then exit.
05-21-2021 09:53 AM
Thanks for the detailed response, Mark. I need to think about the points you made.
Can you point me in the direction of an example of Multithreading in LabView? Some things would become more obvious if I can see prior implementation. Most of the examples that I have seen use While loops, and are not well explained.
05-21-2021 10:19 AM
You could look at examples of the Producer/Consumer architecture. They generally have two loops running. The other thing you could look at are the examples of calling VIs dynamically using the "Start Asynchronous Call".
05-24-2021 12:43 PM
@ooyeniyi wrote:
There are two separate VIs that I would like to run in parallel as different threads.
...
- is this how multi-threading is implemented?
- how can I prove that the threads are indeed on separate threads?
- how can I determine which thread is being executed at any given time?
- is the While Loop necessary? (only included it to create a form of partition. Would it still be multi-threading if the loops were replaced with frame structures instead, or perhaps nothing at all?)
LabVIEW does not create a dedicated thread for each subVI, in fact, LabVIEW creates many threads which are shared among all parts of the code. One thread is the UI thread that handles the front panel. LabVIEW is excellent at balancing the workload and threads.
Now to the other questions:
Are you trying to solve any specific problem? Do you need help programming? I recommend to start a new thread, attach simple example code, and ask detailed questions.