<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Seneca5 Software Blog &#187; threads</title>
	<atom:link href="http://www.seneca5.com/blog/tag/threads/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.seneca5.com/blog</link>
	<description>Expert LabVIEW™ Development</description>
	<lastBuildDate>Mon, 06 Sep 2010 15:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Nice Threads</title>
		<link>http://www.seneca5.com/blog/2009/11/nice-threads/</link>
		<comments>http://www.seneca5.com/blog/2009/11/nice-threads/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 07:33:57 +0000</pubDate>
		<dc:creator>Craig Bedward</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[LabVIEW]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.seneca5.com/blog/?p=202</guid>
		<description><![CDATA[Multithread programming in traditional languages can be a challenge. The programmer is responsible for managing many things, such as: Separating logical tasks Avoiding race conditions Avoiding thread starvation conditions Managing communication between the threads LabVIEW, on the other hand, lends itself easily to multithreaded programming. In fact, if you follow the dataflow rules, multithreading will [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/VI-Server-Functions1.PNG"></a><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/Threads_RGB.png"><img class="alignright size-full wp-image-478" title="Threads" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/Threads_RGB.png" alt="" width="305" height="340" /></a>Multithread programming in traditional languages can be a challenge. The programmer is responsible for managing many things, such as:</p>
<ul>
<li>Separating logical tasks</li>
<li>Avoiding race conditions</li>
<li>Avoiding thread starvation conditions</li>
<li>Managing communication between the threads</li>
</ul>
<p>LabVIEW, on the other hand, lends itself easily to multithreaded programming. In fact, if you follow the dataflow rules, multithreading will happen <em>automatically</em> when LabVIEW finds the opportunity.</p>
<h3><span id="more-202"></span>Dataflow Programming</h3>
<p>LabVIEW follows a programming model called dataflow programming. Rather than writing a program in the form of Step 1, Step 2, Step 3, &#8230;, LabVIEW programs pass the data on wires. Any VI taking inputs must wait for all of its inputs to be ready before it can run. In the example below, Loop A must run before Loop B is able to run, because Loop A produces data that is used by Loop B. The data dependency controls the order of execution.</p>
<div id="attachment_216" class="wp-caption aligncenter" style="width: 466px"><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/loops_dependent.PNG"><img class="size-full wp-image-216" title="loops_dependent" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/loops_dependent.PNG" alt="Loops with a data dependency" width="456" height="140" /></a><p class="wp-caption-text">Loops with a data dependency</p></div>
<p>However, if we remove the data dependency, we no longer force the order of execution. LabVIEW gets to decide which loop will run first. But is it Loop A or Loop B? Actually, it is both. This is where LabVIEW&#8217;s automatic multithreading kicks in. LabVIEW will create multiple threads and run each loop simultaneously, one in each thread.</p>
<div id="attachment_217" class="wp-caption aligncenter" style="width: 466px"><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/loops_threaded.PNG"><img class="size-full wp-image-217" title="loops_threaded" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/loops_threaded.PNG" alt="Loops with no data dependency" width="456" height="140" /></a><p class="wp-caption-text">Loops with no data dependency</p></div>
<h3>Sharing Data</h3>
<p>Remember, LabVIEW can do automatic multithreading only because there is no wired data dependency between the two loops, so the next question is, how do we share data between the two loops?</p>
<h4>Local Variable</h4>
<p>The first way of sharing data between loops that probably comes to mind is a local variable. It provides a way for you to access the value of data without wiring, so it can indeed be used and still allow automatic multithreading. But there are some drawbacks to local variables: they can cause race conditions, they need to be initialized, they break the dataflow programming paradigm, so you should not get used to using them (if you are a LabVIEW purist, you would never use a local variable).</p>
<h4>Queues</h4>
<p>A better way to share data between loops is with a queue. One loop can send a message to the other with a queue without creating a data dependency. And the message is delivered in a more predictable manner than a local variable. A great example of this technique is the standard Producer/Consumer template.</p>
<div id="attachment_219" class="wp-caption aligncenter" style="width: 543px"><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/producer-consumer.PNG"><img class="size-full wp-image-219" title="producer-consumer" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/producer-consumer.PNG" alt="Producer-Consumer Template" width="533" height="371" /></a><p class="wp-caption-text">Producer-Consumer Template</p></div>
<h3>Spinning Off Threads</h3>
<p>Multithreading with multiple parallel loops is fine, but it takes you only so far. Take for example a program that needs to capture data from a DAQ card as fast as possible, handle the user interface at a medium rate to keep up with the operator, and log the data to a file as a background function. Using parallel loops, you cannot control the priority of the various loops, which means the file loop runs as an equal to the DAQ task. That will likely slow down the DAQ and cause data to be lost.</p>
<p>One solution is to put each of the loops into a separate VI and &#8220;spin off&#8221; each of those VI&#8217;s to run independently. You can control the priority of a VI (under <em>VI Properties-&gt;Execution</em>), so you can set the DAQ VI to be time critical and the file I/O to be a background task.</p>
<h3>How to Spin Off a Thread</h3>
<p><img class="alignright size-full wp-image-220" style="margin-left: 10px; margin-right: 10px;" title="AppControlPalette" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/AppControlPalette.PNG" alt="AppControlPalette" width="174" height="225" /></p>
<p>To run a VI as a thread, you can&#8217;t just call it as you would a normal VI. Normally, when a VI calls another, that VI suspends its execution until the sub-VI completes. That&#8217;s the opposite of the multithreaded operation we are after. To spin off a VI, we can make use of some of the <em>VI Server </em>VI&#8217;s. These can be found in the <em>Application Control</em> palette.</p>
<p>The process is not difficult:</p>
<ol>
<li>Open VI Reference</li>
<li>Invoke Node &#8211; <em>Invoke Run VI</em>, specifying to not <em>Wait Until Done</em></li>
<li>(Optionally) Invoke Node &#8211; Invoke <em>FP.Open</em> to show the front panel of the VI you are spinning off</li>
<li>Use a named queue to communicate between the threads</li>
<li>When the thread is terminated, use Close Reference to dispose of the reference to the VI</li>
</ol>
<p><a href="http://www.seneca5.com/blog/wp-content/uploads/2009/11/VI-Server-Functions1.PNG"><img style="display: block; margin-left: auto; margin-right: auto; border: 0px initial initial;" title="VI Server Functions" src="http://www.seneca5.com/blog/wp-content/uploads/2009/11/VI-Server-Functions1.PNG" alt="VI Server Functions" width="536" height="144" /></a></p>
<h4>Priority</h4>
<p>When you spin off a thread, it will run with the priority specified in <em>VI Properties-&gt;Execution</em>. You can also set the <em>Preferred Execution System</em> to tweak performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seneca5.com/blog/2009/11/nice-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
