<?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>Artful Code &#187; pike</title>
	<atom:link href="http://www.artfulcode.net/tags/pike/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Thread pool module for Pike</title>
		<link>http://www.artfulcode.net/articles/thread-pool-module-for-pike/</link>
		<comments>http://www.artfulcode.net/articles/thread-pool-module-for-pike/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 17:14:09 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[pike]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=572</guid>
		<description><![CDATA[Async provides a simple process pool that allocates a number of worker threads
that may then be utilized collectively without having to deal directly with
threads. Results of jobs sent to the pool are "future" objects, called Asyncs.
Asyncs' values are then acquired by calling Async.sync, which blocks until the
value has been set by the pool. A supervisor thread monitors the worker threads
and restarts any that terminate abnormally.]]></description>
			<content:encoded><![CDATA[<h2>Async.pmod</h2>
<p>Download it <a href="http://www.artfulcode.net/wp-content/uploads/2009/03/async.pmod">here</a>.</p>
<p>Async provides a simple process pool that allocates a number of worker threads<br />
that may then be utilized collectively without having to deal directly with<br />
threads. Results of jobs sent to the pool are &#8220;future&#8221; objects, called Asyncs.<br />
Asyncs&#8217; values are then acquired by calling Async.sync, which blocks until the<br />
value has been set by the pool. A supervisor thread monitors the worker threads<br />
and restarts any that terminate abnormally.<span id="more-572"></span></p>
<p>Usage is simple:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create a pool of four processes</span>
Pool pool <span style="color: #339933;">=</span> Pool<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// send a job to the pool</span>
Async result <span style="color: #339933;">=</span> Pool<span style="color: #339933;">-&gt;</span>send<span style="color: #009900;">&#40;</span>exp<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get the result</span>
result<span style="color: #339933;">-&gt;</span>sync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 22026.46484375</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// alternately, you can call an async</span>
result<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 22026.46484375</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// pools can conveniently map across a sequence</span>
Async mapped_result <span style="color: #339933;">=</span> pool<span style="color: #339933;">-&gt;</span>map<span style="color: #009900;">&#40;</span>exp<span style="color: #339933;">,</span> enumerate<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mapped_result<span style="color: #339933;">-&gt;</span>sync<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; ({ 1.0, 2.718281745910645, 7.389056205749512, 20.08553695678711, 54.59814834594727 })</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// or iterate without collecting return values</span>
<span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
pool<span style="color: #339933;">-&gt;</span>iter<span style="color: #009900;">&#40;</span>lambda <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> x <span style="color: #339933;">+=</span> n<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> enumerate<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
write<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// =&gt; 4950</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close the pool, blocking until all workers have terminated</span>
pool<span style="color: #339933;">-&gt;</span>close<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F&amp;title=Thread+pool+module+for+Pike" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F&amp;title=Thread+pool+module+for+Pike" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Thread+pool+module+for+Pike&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F&amp;title=Thread+pool+module+for+Pike" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F&amp;title=Thread+pool+module+for+Pike" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F&amp;title=Thread+pool+module+for+Pike" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Thread+pool+module+for+Pike+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fthread-pool-module-for-pike%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/thread-pool-module-for-pike/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compiling Pike 7.8 on OSX PPC</title>
		<link>http://www.artfulcode.net/articles/compiling-pike-78-on-osx-ppc/</link>
		<comments>http://www.artfulcode.net/articles/compiling-pike-78-on-osx-ppc/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 21:49:12 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[pike]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=540</guid>
		<description><![CDATA[Pike 7.8 did not compile out of the box on my Mac, so I figured I would write up what it took to get it working.]]></description>
			<content:encoded><![CDATA[<p>Pike 7.8 did not compile out of the box on my Mac, so I figured I would write up what it took to get it working.<span id="more-540"></span></p>
<p>The initial problem was getting it to find the MySQL headers and libraries installed by macports, which was easy enough to fix.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">LDCONFIG</span>=<span style="color: #ff0000;">&quot;-L/opt/local/mysql/lib&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> LDCONFIG
<span style="color: #007800;">CPPFLAGS</span>=<span style="color: #ff0000;">&quot;-I/opt/local/include/mysql5&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> CPPFLAGS</pre></div></div>

<p>It found the files it needed, but it still didn&#8217;t pass the test.  It complained about not finding various API functions in the MySQL library. It turns out that it was detecting my system as 64bit (when it is in fact 32).  For that, I had to run make with CONFIGUREARGS:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #007800;">CONFIGUREARGS</span>=<span style="color: #ff0000;">&quot;--with-abi=32&quot;</span></pre></div></div>

<p>&#8230;and that did it.  Thank god for the good folks in #pike on irc.freenode.net!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F&amp;title=Compiling+Pike+7.8+on+OSX+PPC" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F&amp;title=Compiling+Pike+7.8+on+OSX+PPC" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Compiling+Pike+7.8+on+OSX+PPC&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F&amp;title=Compiling+Pike+7.8+on+OSX+PPC" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F&amp;title=Compiling+Pike+7.8+on+OSX+PPC" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F&amp;title=Compiling+Pike+7.8+on+OSX+PPC" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Compiling+Pike+7.8+on+OSX+PPC+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fcompiling-pike-78-on-osx-ppc%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/compiling-pike-78-on-osx-ppc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An overview of threading in Pike</title>
		<link>http://www.artfulcode.net/articles/an-overview-of-threading-in-pike/</link>
		<comments>http://www.artfulcode.net/articles/an-overview-of-threading-in-pike/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 13:17:24 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[pike]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/overview-threading-pike/</guid>
		<description><![CDATA[Pike&#8217;s approach to threading is simple and mindful of the sanity of the programmer. Threading is available for all systems using Unix, POSIX, or Windows threads. The Thread module in the standard library provides a complete system for multi-threading. Initially, a thread is created by instantiating a new instance of the Thread.Thread class with a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://pike.ida.liu.se/">Pike&#8217;s</a> approach to threading is simple and mindful of the sanity of the programmer.  Threading is available for all systems using Unix, POSIX, or Windows threads.<span id="more-13"></span></p>
<p>The <code>Thread</code> module in the standard library provides a complete system for multi-threading.  Initially, a thread is created by instantiating a new instance of the <code>Thread.Thread</code> class with a function that will be executed in a new thread and any arguments to that function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> say_hello<span style="color: #009900;">&#40;</span><span style="color: #993333;">string</span> name<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    write<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, &quot;</span> <span style="color: #339933;">+</span> name <span style="color: #339933;">+</span> <span style="color: #ff0000;">&quot;.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Thread.<span style="color: #202020;">Thread</span> thread<span style="color: #339933;">;</span>
    thread <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Thread</span><span style="color: #009900;">&#40;</span>say_hello<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Jeff&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// return -1 so execution does not terminate</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Pike&#8217;s interpreter is completely thread-safe.  All execution is protected by a global interpreter lock, similar to that of Python.  Unlike Python, however, Pike&#8217;s locking is finely grained and released often, making threads quite useful in Pike.</p>
<h4>Thread.Mutex and Thread.MutexKey</h4>
<p>Mutexes are required to protect critical sections that must be executed atomically.  Thanks to the interpreter lock, no two threads will ever access the same variable concurrently.  However, if a variable is modified or accessed multiple times in a thread, a mutex must be used to guard it:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Thread.<span style="color: #202020;">Mutex</span> m <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Mutex</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// create a mutex</span>
<span style="color: #993333;">void</span> func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Thread.<span style="color: #202020;">MutexKey</span> k <span style="color: #339933;">=</span> m<span style="color: #339933;">-&gt;</span>lock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// acquire the lock</span>
&nbsp;
    ... <span style="color: #666666; font-style: italic;">// do stuff</span>
    ... <span style="color: #666666; font-style: italic;">// do more stuff</span>
&nbsp;
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice that the mutex was never released.  Once the <code>MutexKey</code> (returned by <code>Mutex->lock()</code>) goes out of scope, the mutex is automatically released.  This means that a method can be made to be synchronized simply by acquiring a lock at the beginning of the function.</p>
<h4>Thread.Condition</h4>
<p>Condition variables are used to synchronize events across multiple threads.  A thread controlling a resource holds a condition variable, with other threads waiting for the condition to be signaled.  The controlling thread may chose to signal just one or all waiting threads at once.</p>
<p>A <code>MutexKey</code> must be provided by the waitin thread to guarantee synchronicity with the signaling thread.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Thread.<span style="color: #202020;">Mutex</span> mutex <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Mutex</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> consumer<span style="color: #009900;">&#40;</span>Thread.<span style="color: #202020;">Condition</span> c<span style="color: #339933;">,</span> Thread.<span style="color: #202020;">Condition</span> c2<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Thread.<span style="color: #202020;">MutexKey</span> key <span style="color: #339933;">=</span> mutex<span style="color: #339933;">-&gt;</span>lock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c<span style="color: #339933;">-&gt;</span>wait<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        key <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        write<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Pong!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c2<span style="color: #339933;">-&gt;</span>signal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> producer<span style="color: #009900;">&#40;</span>Thread.<span style="color: #202020;">Condition</span> c<span style="color: #339933;">,</span> Thread.<span style="color: #202020;">Condition</span> c2<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Thread.<span style="color: #202020;">MutexKey</span> key <span style="color: #339933;">=</span> mutex<span style="color: #339933;">-&gt;</span>lock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c2<span style="color: #339933;">-&gt;</span>wait<span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        key <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        write<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Ping!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c<span style="color: #339933;">-&gt;</span>signal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Thread.<span style="color: #202020;">Condition</span> condition1<span style="color: #339933;">,</span> condition2<span style="color: #339933;">;</span>
    Thread.<span style="color: #202020;">Thread</span> prod<span style="color: #339933;">,</span> cons<span style="color: #339933;">;</span>
    condition1 <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Condition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    condition2 <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Condition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    prod <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Thread</span><span style="color: #009900;">&#40;</span>producer<span style="color: #339933;">,</span> condition1<span style="color: #339933;">,</span> condition2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cons <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Thread</span><span style="color: #009900;">&#40;</span>consumer<span style="color: #339933;">,</span> condition1<span style="color: #339933;">,</span> condition2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    condition2<span style="color: #339933;">-&gt;</span>signal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the producer started</span>
    <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// keeps script running</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this way, a condition variable is akin to a simplified <a href="http://en.wikipedia.org/wiki/Counting_semaphore">counting semaphore</a>, which may be incremented n times to signal n waiting threads.</p>
<h4>Thread.Local</h4>
<p>Thread-local storage may be created with <code>Thread.Local</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">string</span> thread_fn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    Thread.<span style="color: #202020;">Local</span> data <span style="color: #339933;">=</span> Thread.<span style="color: #202020;">Local</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    data<span style="color: #339933;">-&gt;</span>set<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> data<span style="color: #339933;">-&gt;</span>get<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Thread.Queue</h4>
<p>Queues are first in, first out containers.<br />
<code>Thread.Queue</code> is expandable, so pushing new elements into the queue will not block:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> consumer<span style="color: #009900;">&#40;</span>Thread.<span style="color: #202020;">Queue</span> q<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">string</span> value <span style="color: #339933;">=</span> q<span style="color: #339933;">-&gt;</span>read<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// block until value available</span>
        do_something_with<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Another interesting method is <code>read_array</code>, which reads as many values as are available.  This is extremely useful.  Reading a batch of work results in less time spend blocking, making active queues much more efficient.</p>
<h4>Thread.Fifo</h4>
<p>The primary difference between the <code>Queue</code> and <code>Fifo</code> classes is that the <code>Fifo</code> class is bounded and will block on writes when full.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> producer<span style="color: #009900;">&#40;</span>Thread.<span style="color: #202020;">Fifo</span> q<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">string</span> value<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        value <span style="color: #339933;">=</span> get_value_from_somewhere<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        q<span style="color: #339933;">-&gt;</span>write<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// blocks until space is available</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>Conclusion</h4>
<p>Pike makes threading easy on programmers.  Pike&#8217;s internals are thread-safe; a programmer never needs to worry about corrupting memory.  Mutually exclusive locks are only used to protect blocks of code that must be executed without interruption, so that only the concurrency of the overall technique must be considered.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F&amp;title=An+overview+of+threading+in+Pike" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F&amp;title=An+overview+of+threading+in+Pike" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=An+overview+of+threading+in+Pike&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F&amp;title=An+overview+of+threading+in+Pike" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F&amp;title=An+overview+of+threading+in+Pike" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F&amp;title=An+overview+of+threading+in+Pike" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+An+overview+of+threading+in+Pike+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fan-overview-of-threading-in-pike%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/an-overview-of-threading-in-pike/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

