LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW Threads?

I am trying to get an understanding of what is a LabVIEW thread when using
LV6 on Win32.

1. If a block diagram has two parallel while loops, is each loop a thread.
2. Is each sub-VI a separate thread? (I don't think so, but I thought I
should ask)
3. If a sub-VI is a loop, and does not have a return terminal, is it a
thread?
4. If a call to a sub-VI starts a thread, how do you kill it?

Thanks. Any answers or references to material on this topic (other than NI
App Note 114) would be appreciated...Ed
0 Kudos
Message 1 of 4
(4,091 Views)
"I am trying to get an understanding of what is a LabVIEW thread when using
LV6 on Win32.


1. If a block diagram has two parallel while loops, is each loop a thread.

No. Code within a block diagram executes within the same thread. Code within a thread executes in a cooperative multi-thread mode where each section of code gets a quanta of time to execute and gives up to the next quanta. A section will give up if there is a disjoint function like wait.
2. Is each sub-VI a separate thread? (I don't think so, but I thought I
should ask)
No. There are a certain number of predefined threads that are available as execution threads for you to use. All vi's that belong to the same thread share that same thread cooperatively.
3. If a sub-VI is a loop, and do
es not have a return terminal, is it a
thread?
No. See above.
4. If a call to a sub-VI starts a thread, how do you kill it?
It does not start a thread therefore you can't kill the thread. you can programatically abort a running VI (see VI server).

See the execution property of a VI to see what thread it is using.
0 Kudos
Message 2 of 4
(4,091 Views)
Before I get to the direct questions, a bit of overview. The LV
execution is controlled by dataflow and the loop/case/sequence
structures. LV can execute the same diagram in many ways. The same VI,
without being recompiled, can run on a single threaded OS such as the
classic Mac OS, on a single CPU multithreaded machine, or on a multi-CPU
multithreaded OS. It does this much like the OS itself does, by
scheduling tasks using a priority based round-robin scheduler. There
can be any number of these scheduling objects, which we call an
execution system. Each execution system has a queue used to execute
items marked to run there.

On a single threaded OS, every execution system is run by the one and
only thread. That thread takes turns cooperatively multitasking between
the different execution systems and the window handling UI tasks. It
picks an execution system, dequeues a task, and when the task releases
control, the thread repeats -- determines the next execution system and
task to execute.

On a single CPU multithreaded OS, each execution defaults to having its
own thread. The OS schedules threads preemptively based upon priorities
and heuristics. I said this is the default because it is possible to
customize this to have more than one thread per execution system. Each
thread sleeps until a task is placed on its queue. At that point the OS
may choose to swap threads based upon priorities. Additionally, the OS
can swap out threads, thus swapping out execution systems at any point.

The default for a multi-CPU multithreaded OS is to allocate M threads
per execution system where M is the number of CPUs. Each of the
execution queues now has multiple threads sleeping waiting for a task.
The OS chooses a thread based upon priorities for each of the CPUs and
determines how long to execute and how often to swap. Again, this is
the default and can be modified, though this is rarely necessary.



> 1. If a block diagram has two parallel while loops, is each loop a thread.

Normally, no. Each thread is one or more tasks and since they are in
the same VI, they will be executing in the same execution system. The
code for both while loops is generated to have cooperative scheduling
opertunities in it. The while loops will multitask based upon delays
and other asynchronous nodes within it. Provided the loops do not have
synchronization functions in them, they will execute relatively
independent of one another sharing the CPU resources. If one of the
loops is executing more frequently than it needs to, the best way to
control this is to place a Wait node or some other synchronization to
help limit the execution speed. You can place each of the loops in
separate threads either by placing the different loops in different VIs
and setting the VIs to execute in different execution systems. You can
also customize the execution system they run in to have more than one
thread. If you could be more specific as to why you want to run them in
different threads I can help determine if this would help and the best
way to do this.

> 2. Is each sub-VI a separate thread? (I don't think so, but I thought I
> should ask)

No. Each VI specifies which execution system it wishes to run in. By
default, it is set to Same as Caller so that it adapts to its caller and
avoids thread swaps. You can set the execution system on the Execution
page of the VI Properties dialog. By setting this, you can essentially
place VIs in their own thread, but this isn't the default since it would
affect performance.

> 3. If a sub-VI is a loop, and does not have a return terminal, is it a
> thread?

Whether or not a subVI has a loop doesn't affect how it is scheduled.

> 4. If a call to a sub-VI starts a thread, how do you kill it?

You use one of the synchronization primitives or some other
communication such a global variable to tell the loop to stop executing.
When the subVI stops executing, the execution system either picks
another task to execute or goes to sleep. It is possible to use the VI
Server to abort VIs that have been run using the Run method, but this
isn't necessarily a good thing to do. It is far better to have loops
and subVIs terminate normally.

>
> Thanks. Any answers or references to material on this topic (other than NI
> App Note 114) would be appreciated...Ed

The last three years there have been presentations on the execution
system of LV. You can find most of these on devzone by going to
zone.ni.com and searching for Inside LabVIEW.

Also feel free to ask more questions or give more background.

Greg McKaskle
Message 3 of 4
(4,091 Views)
Thanks for the feedback.

In standard Windows programming I use a separate thread to call the Windows
ReadFile
API to read data from a serial port. This thread gets suspended waiting for
data, while my
main thread send data out the serial port. I am trying to figure out if this
same technique
is applicable to LV.

If I wanted to do the same thing in LV by calling the VISA Read VI, do I
need a separate
thread?

I will check out your reference to devzone. Thanks...Ed
0 Kudos
Message 4 of 4
(4,091 Views)