<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>shebang</title>
	<atom:link href="http://resonanz.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://resonanz.wordpress.com</link>
	<description>A Programmer's Musings</description>
	<lastBuildDate>Sun, 06 Feb 2011 21:33:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='resonanz.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>shebang</title>
		<link>http://resonanz.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://resonanz.wordpress.com/osd.xml" title="shebang" />
	<atom:link rel='hub' href='http://resonanz.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Lack of posts / Mercurial</title>
		<link>http://resonanz.wordpress.com/2006/10/20/lack-of-posts-mercurial/</link>
		<comments>http://resonanz.wordpress.com/2006/10/20/lack-of-posts-mercurial/#comments</comments>
		<pubDate>Fri, 20 Oct 2006 21:23:36 +0000</pubDate>
		<dc:creator>resonanz</dc:creator>
				<category><![CDATA[mercurial]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://resonanz.wordpress.com/2006/10/20/lack-of-posts-mercurial/</guid>
		<description><![CDATA[No one probably noticed the lack of posts in this blog, but for the last few months my job required me to work on a project abroad and this leaves not much time to write about anything interesting. However I got interested in Mercurial a simple yet powerful version control system. Sadly it is difficult [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=7&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>No one probably noticed the lack of posts in this blog, but for the last few months my job required me to work on a project abroad and this leaves not much time to write about anything interesting. However I got interested in <a href="http://www.selenic.com/mercurial/">Mercurial</a> a simple yet powerful version control system. Sadly it is difficult to use it in the corporate world, because there everything needs to have a GUI (and to be fair on Windows any GUI is probably better than &#8220;cmd.com&#8221;). So I started to play a bit with <a href="http://www.riverbankcomputing.co.uk/pyqt/">PyQt</a> (since Mercurial is using <a href="http://www.python.org">Python</a> as the implementation language) and came up with an almost useless prototype. Maybe one day it might be useful but so far it merely shows the repository contents with changed items in different styles and allows filtering the tree. I hope I find the time to work more on this GUI because  I would really like to use Mercurial at work.</p>
<p><a href="http://resonanz.files.wordpress.com/2006/10/screenshot-hgui.png" title="hgui"><img src="http://resonanz.files.wordpress.com/2006/10/screenshot-hgui.thumbnail.png?w=480" alt="hgui" /></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/resonanz.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/resonanz.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/resonanz.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/resonanz.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/resonanz.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=7&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://resonanz.wordpress.com/2006/10/20/lack-of-posts-mercurial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/245960a84e00bbf3c50254f0dd0a0f2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">resonanz</media:title>
		</media:content>

		<media:content url="http://resonanz.files.wordpress.com/2006/10/screenshot-hgui.thumbnail.png" medium="image">
			<media:title type="html">hgui</media:title>
		</media:content>
	</item>
		<item>
		<title>Futures again</title>
		<link>http://resonanz.wordpress.com/2006/08/25/futures-again/</link>
		<comments>http://resonanz.wordpress.com/2006/08/25/futures-again/#comments</comments>
		<pubDate>Fri, 25 Aug 2006 00:06:20 +0000</pubDate>
		<dc:creator>resonanz</dc:creator>
				<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Cplusplus]]></category>

		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/25/futures-again/</guid>
		<description><![CDATA[To see how useful the previously described future is, I tried to retrofit some existing code. Here are some lines of test code before futures came in: 1 obc::image orig(filename); 2 obc::image img = obc::rgb2gray(orig); 3 4 obc::show_image(img, "original"); 5 obc::show_image(obc::contrast_incr(img, 0.15), "contrast_incr"); 6 7 timespec timer_s, timer_e; 8 clock_gettime(CLOCK_REALTIME, &#38;timer_s); 9 10 CvSeq* lines [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=6&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To see how useful the <a href="http://resonanz.wordpress.com/2006/08/23/c-futures/">previously</a> described future is, I tried to retrofit some existing code. Here are some lines of test code before futures came in:</p>
<pre><font color="#a9a9a9"> 1 </font>obc::image orig(filename);
<font color="#a9a9a9"> 2 </font>obc::image img = obc::rgb2gray(orig);
<font color="#a9a9a9"> 3 </font>
<font color="#a9a9a9"> 4 </font>obc::show_image(img, <font color="#8b008b">"original"</font>);
<font color="#a9a9a9"> 5 </font>obc::show_image(obc::contrast_incr(img, <font color="#ff0000"><b>0.15</b></font>), <font color="#8b008b">"contrast_incr"</font>);
<font color="#a9a9a9"> 6 </font>
<font color="#a9a9a9"> 7 </font>timespec timer_s, timer_e;
<font color="#a9a9a9"> 8 </font>clock_gettime(CLOCK_REALTIME, &amp;timer_s);
<font color="#a9a9a9"> 9 </font>
<i><font color="#a9a9a9">10 </font>CvSeq* lines = obc::hough_transform(img);</i>
<font color="#a9a9a9">11 </font>
<font color="#a9a9a9">12 </font>obc::image temp(img);
<font color="#a9a9a9">13 </font>obc::image disp = obc::gray2rgb(temp);
<font color="#a9a9a9">14 </font>obc::draw_hough_lines(disp, <i>lines</i>);
<font color="#a9a9a9">15 </font>
<font color="#a9a9a9">16 </font>clock_gettime(CLOCK_REALTIME, &amp;timer_e);
<font color="#a9a9a9">17 </font>cout &lt;&lt; <font color="#8b008b">"elapsed time = "</font> &lt;&lt; (time_diff(timer_s, timer_e) / <font color="#ff0000"><b>1000000.0</b></font>) &lt;&lt; endl;
<font color="#a9a9a9">18 </font>
<font color="#a9a9a9">19 </font>obc::show_image(disp, <font color="#8b008b">"result"</font>);
<font color="#a9a9a9">20 </font>
<font color="#a9a9a9">21 </font>cvReleaseMemStorage(&amp;<i>lines</i>-&gt;storage);</pre>
<p>The calculation of the Hough transform (line 10) takes a while so considering that each show_image() function needs user feedback which is quite slow it would make sense to trigger the calculation of the transform before the available images are shown. Thus line 5 in the code sample below contains the future that calculates asynchronously the Hough transform while we look at the  images presented to us in the show_image() functions.</p>
<pre><font color="#a9a9a9"> 1 </font>obc::image orig(filename);
<font color="#a9a9a9"> 2 </font>obc::image img = obc::rgb2gray(orig);
<font color="#a9a9a9"> 3 </font>
<font color="#a9a9a9"> 4 </font>obc::image contr = obc::contrast_incr(img, <font color="#ff0000"><b>0.15</b></font>);
<i><font color="#a9a9a9"> 5 </font>concur::future&lt;CvSeq*&gt; lines_f(bind(obc::hough_transform, img));</i>
<font color="#a9a9a9"> 6 </font>
<font color="#a9a9a9"> 7 </font>obc::show_image(img, <font color="#8b008b">"original"</font>);
<font color="#a9a9a9"> 8 </font>obc::show_image(contr, <font color="#8b008b">"contrast_incr"</font>);
<font color="#a9a9a9"> 9 </font>
<font color="#a9a9a9">10 </font>timespec timer_s, timer_e;
<font color="#a9a9a9">11 </font>clock_gettime(CLOCK_REALTIME, &amp;timer_s);
<font color="#a9a9a9">12 </font>
<font color="#a9a9a9">13 </font>obc::image temp(img);
<font color="#a9a9a9">14 </font>obc::image disp = obc::gray2rgb(temp);
<font color="#a9a9a9">15 </font>obc::draw_hough_lines(disp, <i>lines_f()</i>);
<font color="#a9a9a9">16 </font>
<font color="#a9a9a9">17 </font>clock_gettime(CLOCK_REALTIME, &amp;timer_e);
<font color="#a9a9a9">18 </font>cout &lt;&lt; <font color="#8b008b">"elapsed time = "</font> &lt;&lt; (time_diff(timer_s, timer_e) / <font color="#ff0000"><b>1000000.0</b></font>) &lt;&lt; endl;
<font color="#a9a9a9">19 </font>
<font color="#a9a9a9">20 </font>obc::show_image(disp, <font color="#8b008b">"result"</font>);
<font color="#a9a9a9">21 </font>
<font color="#a9a9a9">22 </font>cvReleaseMemStorage(&amp;<i>lines_f()</i>-&gt;storage);</pre>
<p>Now you may have recognized the little timing code and it comes as no surprise that the gains are significant. The old version takes about 2.8 seconds whereas the new one uses only 0.4 seconds for the covered code paths. A little reordering and renaming and voila &#8211; a nice result.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/resonanz.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/resonanz.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/resonanz.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/resonanz.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/resonanz.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=6&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://resonanz.wordpress.com/2006/08/25/futures-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/245960a84e00bbf3c50254f0dd0a0f2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">resonanz</media:title>
		</media:content>
	</item>
		<item>
		<title>C++0x articles</title>
		<link>http://resonanz.wordpress.com/2006/08/25/c0x-articles/</link>
		<comments>http://resonanz.wordpress.com/2006/08/25/c0x-articles/#comments</comments>
		<pubDate>Thu, 24 Aug 2006 22:00:02 +0000</pubDate>
		<dc:creator>resonanz</dc:creator>
				<category><![CDATA[Cplusplus]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/25/c0x-articles/</guid>
		<description><![CDATA[informit.com has some nice articles about what to expect from C++0x. Interesting read. I also noticed that Pete Becker wrote a book about the TR1 standard library additions. Just ordered my copy from Amazon. Luckily, even tough C++ standardization takes quite a long time, most of the proposed changes are available from Boost (where many [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=5&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.informit.com/guides/content.asp?g=cplusplus&amp;seqNum=216">informit.com</a> has some nice articles about what to expect from C++0x. Interesting read. I also noticed that Pete Becker wrote a <a href="http://www.amazon.com/gp/product/0321412990/ref=bxgy_cc_text_b/002-9969033-2794404?ie=UTF8">book</a> about the TR1 standard library additions. Just ordered my copy from Amazon. Luckily, even tough C++ standardization takes quite a long time, most of the proposed changes are available from <a href="http://www.boost.org">Boost</a> (where many of them were initially created) or they even found their way into <a href="http://gcc.gnu.org">mainstream compilers</a> already.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/resonanz.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/resonanz.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/resonanz.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/resonanz.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/resonanz.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=5&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://resonanz.wordpress.com/2006/08/25/c0x-articles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/245960a84e00bbf3c50254f0dd0a0f2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">resonanz</media:title>
		</media:content>
	</item>
		<item>
		<title>C++ Futures</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/</link>
		<comments>http://resonanz.wordpress.com/2006/08/23/c-futures/#comments</comments>
		<pubDate>Wed, 23 Aug 2006 09:52:01 +0000</pubDate>
		<dc:creator>resonanz</dc:creator>
				<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Cplusplus]]></category>

		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/</guid>
		<description><![CDATA[Recently I learnt about Alice, a dialect of Standard ML and thus a mostly functional language. What Alice adds to ML is excellent support for high-level concurrency primitives such as Futures. &#8220;A Future is a mechanism which can be used to provide values that will be resolved to another value asynchronously. Futures can also be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=4&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I learnt about <a href="http://www.ps.uni-sb.de/alice/" title="Alice Homepage">Alice</a>, a dialect of Standard ML and thus a mostly functional language. What Alice adds to ML is excellent support for high-level concurrency primitives such as <a href="http://en.wikipedia.org/wiki/Future_%28programming%29" title="Wikipedia entry">Futures</a>.<a href="http://c2.com/cgi/wiki?FutureValue"></a></p>
<blockquote><p>&#8220;<em>A </em><em>Future</em><em> is a mechanism which can be used to provide values that will be <em>resolved</em> to another value asynchronously. Futures can also be called &#8220;promises&#8221; (see <a href="http://c2.com/cgi/wiki?PromisePipelining">PromisePipelining</a>). A method/function can return a future to its caller, and continue to compute the value that the future will resolve to in another thread of control</em> [...]&#8221; <a href="http://c2.com/cgi/wiki?FutureValue">Wiki</a></p></blockquote>
<p>Alice&#8217;s syntax for futures is especially pleasing. To give an example, an expression like <span>&#8216;spawn exp;</span>&#8216; would evaluate &#8216;exp&#8217; asynchronously in a new thread and return immediately. Requesting the value of &#8216;exp&#8217; results in implicit synchronization with the thread evaluating the result until completed. Later invocations return the result directly.</p>
<p>Alice is not the only language supporting futures. Even Java version 1.5 got a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/FutureTask.html">FutureTask</a> although the usual restrictions of Java apply here too.</p>
<p>I could not find an implementation of a future for C++ maybe because multithreading is not standardized yet. However it is not difficult to implement one. I will use <a href="http://www.boost.org/libs/thread/doc/index.html">Boost.Threads</a> as the underlying threading framework, but other libraries are just as good for this particular task.</p>
<p>Please note that the definition of futures does not specify if the computation of the given expression would start immediately or only when the result is requested the first time. I let the evaluation begin on future creation but an explicit start function could be added easily.</p>
<p>Now here is the complete future implementation (Did I mention than <a href="http://www.vim.org">Vim</a>&#8216;s HTML export is really handy?):</p>
<pre>
<font color="#a9a9a9"> 1 </font><font color="#00008b"><strong>#ifndef FUTURE_HPP</strong></font>
<font color="#a9a9a9"> 2 </font><font color="#00008b"><strong>#define FUTURE_HPP</strong></font>
<font color="#a9a9a9"> 3 </font>
<font color="#a9a9a9"> 4 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;tr1/functional&gt;</font>
<font color="#a9a9a9"> 5 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;boost/bind.hpp&gt;</font>
<font color="#a9a9a9"> 6 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;boost/utility.hpp&gt;</font>
<font color="#a9a9a9"> 7 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;boost/thread/mutex.hpp&gt;</font>
<font color="#a9a9a9"> 8 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;boost/thread/thread.hpp&gt;</font>
<font color="#a9a9a9"> 9 </font>
<font color="#a9a9a9">10 </font><font color="#8b0000"><strong>using</strong></font> std::tr1::function;
<font color="#a9a9a9">11 </font><font color="#8b0000"><strong>using</strong></font> boost::thread;
<font color="#a9a9a9">12 </font><font color="#8b0000"><strong>using</strong></font> boost::mutex;
<font color="#a9a9a9">13 </font>
<font color="#a9a9a9">14 </font><font color="#0000ff"><strong>template</strong></font>&lt;<font color="#0000ff"><strong>typename</strong></font> T&gt;
<font color="#a9a9a9">15 </font><font color="#0000ff"><strong>class</strong></font> future : boost::noncopyable
<font color="#a9a9a9">16 </font>{
<font color="#a9a9a9">17 </font><font color="#8b0000"><strong>public</strong></font>:
<font color="#a9a9a9">18 </font>        future(function&lt;T ()&gt; <font color="#0000ff"><strong>const</strong></font> &amp; f);
<font color="#a9a9a9">19 </font>        T <font color="#8b0000"><strong>operator</strong></font>()();
<font color="#a9a9a9">20 </font>
<font color="#a9a9a9">21 </font><font color="#8b0000"><strong>private</strong></font>:
<font color="#a9a9a9">22 </font>        <font color="#0000ff"><strong>void</strong></font> run();
<font color="#a9a9a9">23 </font>
<font color="#a9a9a9">24 </font>        T v_;
<font color="#a9a9a9">25 </font>        function&lt;T ()&gt; f_;
<font color="#a9a9a9">26 </font>        thread t_;
<font color="#a9a9a9">27 </font>        mutex m_;
<font color="#a9a9a9">28 </font>        <font color="#0000ff"><strong>bool</strong></font> joined_;
<font color="#a9a9a9">29 </font>};
<font color="#a9a9a9">30 </font>
<font color="#a9a9a9">31 </font><font color="#0000ff"><strong>template</strong></font>&lt;<font color="#0000ff"><strong>typename</strong></font> T&gt;
<font color="#a9a9a9">32 </font>future&lt;T&gt;::future(function&lt;T ()&gt; <font color="#0000ff"><strong>const</strong></font> &amp; f)
<font color="#a9a9a9">33 </font>: v_(), f_(f), t_(boost::bind(&amp;future&lt;T&gt;::run, <font color="#8b0000"><strong>this</strong></font>)), joined_(<font color="#8b008b">false</font>) {}
<font color="#a9a9a9">34 </font>
<font color="#a9a9a9">35 </font><font color="#0000ff"><strong>template</strong></font>&lt;<font color="#0000ff"><strong>typename</strong></font> T&gt;
<font color="#a9a9a9">36 </font>T future&lt;T&gt;::<font color="#8b0000"><strong>operator</strong></font>()()
<font color="#a9a9a9">37 </font>{
<font color="#a9a9a9">38 </font>        mutex::scoped_lock lock(m_);
<font color="#a9a9a9">39 </font>        <font color="#8b0000"><strong>if</strong></font> (!joined_) {
<font color="#a9a9a9">40 </font>                t_.join();
<font color="#a9a9a9">41 </font>                joined_ = <font color="#8b008b">true</font>;
<font color="#a9a9a9">42 </font>        }
<font color="#a9a9a9">43 </font>        <font color="#8b0000"><strong>return</strong></font> v_;
<font color="#a9a9a9">44 </font>}
<font color="#a9a9a9">45 </font>
<font color="#a9a9a9">46 </font><font color="#0000ff"><strong>template</strong></font>&lt;<font color="#0000ff"><strong>typename</strong></font> T&gt;
<font color="#a9a9a9">47 </font><font color="#0000ff"><strong>void</strong></font> future&lt;T&gt;::run()
<font color="#a9a9a9">48 </font>{
<font color="#a9a9a9">49 </font>        v_ = f_();
<font color="#a9a9a9">50 </font>}
<font color="#a9a9a9">51 </font>
<font color="#a9a9a9">52 </font><font color="#00008b"><strong>#endif</strong></font> <font color="#006400">// FUTURE_HPP</font>
<font color="#a9a9a9">53 </font></pre>
<p>I think the code is reasonably clear. We take a tr1::function and immediately start a thread evaluating the function. Now it should be obvious that the expressive power of this future is not as high as Alice&#8217;s since we can only evaluate functions, member functions or functors (classes that overload operator()()) and not simple expressions like 2 + 3 but most non-trivial code is contained in  functions anyway. If one of my favourite C++0x proposals makes it into the next standard revision we may even adapt the future to handle <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1968.pdf">lambda expressions</a>.</p>
<p>Now turning to the client side it is trivial to use the future for asynchronous evaluation. In the sample code below I simulate work with sleep(). Two functions (func1() and func2()) do heavy work and two other functions use these functions in futures. The first one (immediate_request()) requests the value of the future immediately and thus blocks until the future finishes evaluating the result. The second call returns the result then immediately. The function doing_work_itm() also creates a future but works hard before requesting the result which is already available by the time the work was done (timing issues aside).</p>
<pre><font color="#a9a9a9"> 1 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;iostream&gt;</font>
<font color="#a9a9a9"> 2 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;unistd.h&gt;</font>
<font color="#a9a9a9"> 3 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">&lt;boost/thread/thread.hpp&gt;</font>
<font color="#a9a9a9"> 4 </font><font color="#00008b"><strong>#include </strong></font><font color="#8b008b">"future.hpp"</font>
<font color="#a9a9a9"> 5 </font>
<font color="#a9a9a9"> 6 </font><font color="#8b0000"><strong>using</strong></font> std::cout;
<font color="#a9a9a9"> 7 </font><font color="#8b0000"><strong>using</strong></font> std::endl;
<font color="#a9a9a9"> 8 </font><font color="#8b0000"><strong>using</strong></font> std::flush;
<font color="#a9a9a9"> 9 </font>
<font color="#a9a9a9">10 </font><font color="#0000ff"><strong>int</strong></font> func1() { sleep(<font color="#ff0000"><strong>15</strong></font>); <font color="#8b0000"><strong>return</strong></font> <font color="#ff0000"><strong>42</strong></font>; }
<font color="#a9a9a9">11 </font><font color="#0000ff"><strong>int</strong></font> func2() { sleep(<font color="#ff0000"><strong>15</strong></font>); <font color="#8b0000"><strong>return</strong></font> <font color="#ff0000"><strong>42</strong></font>; }
<font color="#a9a9a9">12 </font>
<font color="#a9a9a9">13 </font><font color="#0000ff"><strong>void</strong></font> immediate_request()
<font color="#a9a9a9">14 </font>{
<font color="#a9a9a9">15 </font>        future&lt;<font color="#0000ff"><strong>int</strong></font>&gt; f1(func1);
<font color="#a9a9a9">16 </font>        cout &lt;&lt; <font color="#8b008b">"Requesting result of f1 ..."</font> &lt;&lt; endl;
<font color="#a9a9a9">17 </font>        cout &lt;&lt; <font color="#8b008b">"f1 = "</font> &lt;&lt; f1() &lt;&lt; endl;
<font color="#a9a9a9">18 </font>}
<font color="#a9a9a9">19 </font>
<font color="#a9a9a9">20 </font><font color="#0000ff"><strong>void</strong></font> doing_work_itm()
<font color="#a9a9a9">21 </font>{
<font color="#a9a9a9">22 </font>        future&lt;<font color="#0000ff"><strong>int</strong></font>&gt; f2(func2);
<font color="#a9a9a9">23 </font>        cout &lt;&lt; <font color="#8b008b">"Doing work ..."</font> &lt;&lt; endl;
<font color="#a9a9a9">24 </font>        sleep(<font color="#ff0000"><strong>15</strong></font>);
<font color="#a9a9a9">25 </font>        cout &lt;&lt; <font color="#8b008b">"Requesting result of f2 ..."</font> &lt;&lt; endl;
<font color="#a9a9a9">26 </font>        cout &lt;&lt; <font color="#8b008b">"f2 = "</font> &lt;&lt; f2() &lt;&lt; endl;
<font color="#a9a9a9">27 </font>}
<font color="#a9a9a9">28 </font>
<font color="#a9a9a9">29 </font><font color="#0000ff"><strong>int</strong></font> main()
<font color="#a9a9a9">30 </font>{
<font color="#a9a9a9">31 </font>        boost::thread t1(immediate_request);
<font color="#a9a9a9">32 </font>        boost::thread t2(doing_work_itm);
<font color="#a9a9a9">33 </font>        t1.join();
<font color="#a9a9a9">34 </font>        t2.join();
<font color="#a9a9a9">35 </font>        <font color="#8b0000"><strong>return</strong></font> <font color="#ff0000"><strong>0</strong></font>;
<font color="#a9a9a9">36 </font>}
<font color="#a9a9a9">37 </font></pre>
<p>The nice thing about using futures is obviously the implicit data driven synchronization.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/resonanz.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/resonanz.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/resonanz.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/resonanz.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/resonanz.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=resonanz.wordpress.com&amp;blog=244556&amp;post=4&amp;subd=resonanz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://resonanz.wordpress.com/2006/08/23/c-futures/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/245960a84e00bbf3c50254f0dd0a0f2c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">resonanz</media:title>
		</media:content>
	</item>
	</channel>
</rss>
