<?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>Ponton&#039;s site &#187; list</title>
	<atom:link href="http://www.ponton.srednikpe.org/tag/list/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ponton.srednikpe.org</link>
	<description>blog głównie matematyczno-informatyczny (nie chce mi się pisać o życiu)</description>
	<lastBuildDate>Mon, 18 Jan 2010 22:23:48 +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>boost::thread and std::list</title>
		<link>http://www.ponton.srednikpe.org/2009/06/22/boostthread-and-stdlist/</link>
		<comments>http://www.ponton.srednikpe.org/2009/06/22/boostthread-and-stdlist/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 01:28:49 +0000</pubDate>
		<dc:creator>ponton</dc:creator>
				<category><![CDATA[Computer science and math]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[my code]]></category>
		<category><![CDATA[stl]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://www.ponton.srednikpe.org/?p=243</guid>
		<description><![CDATA[I had to create many threads in C++. Of course I didn&#8217;t want to use pthread, because it&#8217;s ugly C. I chose boost::thread instead. Then I needed some container to keep all these threads. Arrays were out of the question for the same reason as pthread. I could use boost:array but stl::list was easier. The [...]]]></description>
			<content:encoded><![CDATA[<p>I had to create many threads in C++. Of course I didn&#8217;t want to use pthread, because it&#8217;s ugly C. I chose boost::thread instead. Then I needed some container to keep all these threads. Arrays were out of the question for the same reason as pthread. I could use boost:array but stl::list was easier.</p>
<p>The problem was&#8212;how to keep this thread objects? I couldn&#8217;t use std::list&lt;boost::thread&gt; nor std::list&lt;boost::thread &amp;&gt; because threads cannot be copied. I had to dynamically create the object in order to prevent the destruction after the scope. But for some reason I got compiler error while trying std::list&lt; std::auto_ptr&lt;boost::thread&gt; &gt; in the line where I pushed_back newly created threads.</p>
<p>The cause of the error was that the argument of std::list::push_back() was a constant reference. And while assinging one auto_ptr to another, the first one is getting call release() method, but it can&#8217;t since the reference is constant and release() change the state of the object.</p>
<p>To solve this problem one can use boost::shared_ptr.</p>
<p>Example:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;list&gt;</span>
<span style="color: #339900;">#include &lt;boost/thread.hpp&gt;</span>
<span style="color: #339900;">#include &lt;boost/date_time.hpp&gt;</span>
<span style="color: #339900;">#include &lt;boost/smart_ptr.hpp&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> std<span style="color: #008080;">::</span><span style="color: #007788;">list</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">using</span> std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">using</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> foo
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">void</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> id<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;starting thread &quot;</span> <span style="color: #000080;">&lt;&lt;</span> id <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008080;">;</span>
		boost<span style="color: #008080;">::</span><span style="color: #007788;">this_thread</span><span style="color: #008080;">::</span><span style="color: #007788;">sleep</span><span style="color: #008000;">&#40;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">posix_time</span><span style="color: #008080;">::</span><span style="color: #007788;">seconds</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;ending thread &quot;</span> <span style="color: #000080;">&lt;&lt;</span> id <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">typedef</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">shared_ptr</span><span style="color: #000080;">&lt;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #000080;">&gt;</span> thread_ptr<span style="color: #008080;">;</span>
	list<span style="color: #000080;">&lt;</span>thread_ptr<span style="color: #000080;">&gt;</span> thread<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;creating threads<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		thread.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">shared_ptr</span><span style="color: #000080;">&lt;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span>foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;after creation, now joining them<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>list<span style="color: #000080;">&lt;</span>thread_ptr<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> it <span style="color: #000080;">=</span> thread.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> it <span style="color: #000040;">!</span><span style="color: #000080;">=</span> thread.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>it<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>it<span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>join<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;ending program&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And the result:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">tomek<span style="color: #000000; font-weight: bold;">@</span>notlob:~<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">g++</span> <span style="color: #660033;">-o</span> threads threads.cpp <span style="color: #660033;">-O2</span> <span style="color: #660033;">-Wall</span> -lboost_thread-mt
tomek<span style="color: #000000; font-weight: bold;">@</span>notlob:~<span style="color: #000000; font-weight: bold;">%</span> .<span style="color: #000000; font-weight: bold;">/</span>threads
creating threads
starting thread <span style="color: #000000;">1</span>
starting thread <span style="color: #000000;">0</span>
starting thread <span style="color: #000000;">2</span>
starting thread <span style="color: #000000;">3</span>
starting thread starting thread <span style="color: #000000;">4</span>
<span style="color: #000000;">5</span>
starting thread <span style="color: #000000;">6</span>
starting thread <span style="color: #000000;">7</span>
starting thread <span style="color: #000000;">8</span>
starting thread <span style="color: #000000;">9</span>
after creation, now joining them
ending thread <span style="color: #000000;">1</span>
ending thread <span style="color: #000000;">3</span>
ending thread <span style="color: #000000;">5</span>
ending thread <span style="color: #000000;">4</span>
ending thread <span style="color: #000000;">0</span>
ending thread <span style="color: #000000;">2</span>
ending thread <span style="color: #000000;">6</span>
ending thread <span style="color: #000000;">8</span>
ending thread <span style="color: #000000;">7</span>
ending thread <span style="color: #000000;">9</span>
ending program</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ponton.srednikpe.org/2009/06/22/boostthread-and-stdlist/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
