<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: C++ Futures</title>
	<atom:link href="http://resonanz.wordpress.com/2006/08/23/c-futures/feed/" rel="self" type="application/rss+xml" />
	<link>http://resonanz.wordpress.com/2006/08/23/c-futures/</link>
	<description>A Programmer's Musings</description>
	<lastBuildDate>Thu, 20 Dec 2007 13:37:00 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Maximus</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-850</link>
		<dc:creator>Maximus</dc:creator>
		<pubDate>Thu, 20 Dec 2007 13:37:00 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-850</guid>
		<description>I would like to see a continuation of the topic</description>
		<content:encoded><![CDATA[<p>I would like to see a continuation of the topic</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: freeeeringtones</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-651</link>
		<dc:creator>freeeeringtones</dc:creator>
		<pubDate>Sat, 18 Aug 2007 02:33:07 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-651</guid>
		<description>popular free ringtones 
 
www.thehotstop.info 
 
signature...</description>
		<content:encoded><![CDATA[<p>popular free ringtones </p>
<p><a href="http://www.thehotstop.info" rel="nofollow">http://www.thehotstop.info</a> </p>
<p>signature&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: casinosfreebonusesv</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-621</link>
		<dc:creator>casinosfreebonusesv</dc:creator>
		<pubDate>Sun, 12 Aug 2007 03:48:10 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-621</guid>
		<description>deposit free bonus casino 
see to signature...</description>
		<content:encoded><![CDATA[<p>deposit free bonus casino<br />
see to signature&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NowWhorgo</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-473</link>
		<dc:creator>NowWhorgo</dc:creator>
		<pubDate>Mon, 25 Jun 2007 00:40:50 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-473</guid>
		<description>&lt;a href=&quot;http://ford2007.freehostia.com/ford-dealerships-in-houston-tx.html&quot; rel=&quot;nofollow&quot;&gt;ford dealerships in houston tx&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p><a href="http://ford2007.freehostia.com/ford-dealerships-in-houston-tx.html" rel="nofollow">ford dealerships in houston tx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: resonanz</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-4</link>
		<dc:creator>resonanz</dc:creator>
		<pubDate>Fri, 20 Oct 2006 20:50:46 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-4</guid>
		<description>Updated the future code.</description>
		<content:encoded><![CDATA[<p>Updated the future code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: resonanz</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-3</link>
		<dc:creator>resonanz</dc:creator>
		<pubDate>Tue, 29 Aug 2006 14:14:01 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-3</guid>
		<description>Thanks for your comment Gert-Jan. Joining the thread seems indeed a possible and better alternative. However there are some things to consider.

Boost.Threads on Unix systems is layered on top of POSIX threads, i.e. boost::thread::join() calls pthread_join() which is not thread-safe:

&quot;The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined.&quot; (http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_join.html)

So we need a mutex to synchronize thread access in operator()().

The other issue is that joining the thread requires that the thread is joinable, i.e. join() should be called only once.

With these issues in mind we could add a mutex lock to operator()() and ensure that join() is called exactly once, e.g.

template
T future::operator()()
{
        mutex::scoped_lock lock(m_);
        if (!joined_) {
                t_.join();
                joined_ = true;
        }
        return v_;
}

where the instance variable &#039;joined_&#039; is a simple boolean value initialized to false.

Other than that I like this version using join(). It does away the additional lock, the enum and the busy wait - thanks again.</description>
		<content:encoded><![CDATA[<p>Thanks for your comment Gert-Jan. Joining the thread seems indeed a possible and better alternative. However there are some things to consider.</p>
<p>Boost.Threads on Unix systems is layered on top of POSIX threads, i.e. boost::thread::join() calls pthread_join() which is not thread-safe:</p>
<p>&#8220;The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined.&#8221; (<a href="http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_join.html" rel="nofollow">http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_join.html</a>)</p>
<p>So we need a mutex to synchronize thread access in operator()().</p>
<p>The other issue is that joining the thread requires that the thread is joinable, i.e. join() should be called only once.</p>
<p>With these issues in mind we could add a mutex lock to operator()() and ensure that join() is called exactly once, e.g.</p>
<p>template<br />
T future::operator()()<br />
{<br />
        mutex::scoped_lock lock(m_);<br />
        if (!joined_) {<br />
                t_.join();<br />
                joined_ = true;<br />
        }<br />
        return v_;<br />
}</p>
<p>where the instance variable &#8216;joined_&#8217; is a simple boolean value initialized to false.</p>
<p>Other than that I like this version using join(). It does away the additional lock, the enum and the busy wait &#8211; thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gert-Jan</title>
		<link>http://resonanz.wordpress.com/2006/08/23/c-futures/#comment-2</link>
		<dc:creator>Gert-Jan</dc:creator>
		<pubDate>Tue, 29 Aug 2006 12:37:32 +0000</pubDate>
		<guid isPermaLink="false">https://resonanz.wordpress.com/2006/08/23/c-futures/#comment-2</guid>
		<description>I really like the idea of a future. Nice example too, but why not just:

template 
class future : boost::noncopyable
{
public:
	explicit future(function const &amp; f) : v_(), f_(f), t_(boost::bind(&amp;future::run(), this)) {}
        T operator()() { t_.join(); return v_; }

private:
	void run() { v_ = f_(); }

	T v_;
	function f_;
	thread t_;
};

You see: thread.join() implements the thread state synchronization for you. op() just join()s and returns v_.</description>
		<content:encoded><![CDATA[<p>I really like the idea of a future. Nice example too, but why not just:</p>
<p>template<br />
class future : boost::noncopyable<br />
{<br />
public:<br />
	explicit future(function const &amp; f) : v_(), f_(f), t_(boost::bind(&amp;future::run(), this)) {}<br />
        T operator()() { t_.join(); return v_; }</p>
<p>private:<br />
	void run() { v_ = f_(); }</p>
<p>	T v_;<br />
	function f_;<br />
	thread t_;<br />
};</p>
<p>You see: thread.join() implements the thread state synchronization for you. op() just join()s and returns v_.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
