<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[boost::thread mit moveable Objekt]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich verwende zur Zeit boost 1.46.1.<br />
Da das Starten eines Threads für ein Moveable-Objekt in boost 1.46.1 scheinbar noch nicht implementiert ist, möchte ich gerne wissen wie ihr folgenden Code umsetzen würdet.</p>
<pre><code class="language-cpp">#include &lt;boost/thread.hpp&gt;
#include &lt;iostream&gt;

class Counter {
public:
        Counter(int i) : i(i) {}
        Counter(Counter&amp;&amp; counter){
                this-&gt;i = counter.i;
                counter.i = 0;
        }
        void operator()(){
                for(int j=0; j&lt;10000; ++j){
                        std::cout &lt;&lt; j*i &lt;&lt; std::endl;
                }
        }
private:
        int i;
};

int main(){
        for(int i=0; i&lt;10; ++i){
                Counter counter(i);
                boost::thread thread(counter);
        }
}
</code></pre>
<p>Er kompiliert leider nicht, da Counter keinen Copy-Konstruktor hat.</p>
<p>Würdet ihr das Objekt auf dem Heap erzeugen und einen Smart-Pointer benutzen oder geht es auch anders?</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/287552/boost-thread-mit-moveable-objekt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 20 Aug 2026 10:25:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/287552.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 30 May 2011 20:00:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to boost::thread mit moveable Objekt on Mon, 30 May 2011 20:00:07 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Ich verwende zur Zeit boost 1.46.1.<br />
Da das Starten eines Threads für ein Moveable-Objekt in boost 1.46.1 scheinbar noch nicht implementiert ist, möchte ich gerne wissen wie ihr folgenden Code umsetzen würdet.</p>
<pre><code class="language-cpp">#include &lt;boost/thread.hpp&gt;
#include &lt;iostream&gt;

class Counter {
public:
        Counter(int i) : i(i) {}
        Counter(Counter&amp;&amp; counter){
                this-&gt;i = counter.i;
                counter.i = 0;
        }
        void operator()(){
                for(int j=0; j&lt;10000; ++j){
                        std::cout &lt;&lt; j*i &lt;&lt; std::endl;
                }
        }
private:
        int i;
};

int main(){
        for(int i=0; i&lt;10; ++i){
                Counter counter(i);
                boost::thread thread(counter);
        }
}
</code></pre>
<p>Er kompiliert leider nicht, da Counter keinen Copy-Konstruktor hat.</p>
<p>Würdet ihr das Objekt auf dem Heap erzeugen und einen Smart-Pointer benutzen oder geht es auch anders?</p>
<p>Gruß,<br />
XSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071182</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071182</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Mon, 30 May 2011 20:00:07 GMT</pubDate></item><item><title><![CDATA[Reply to boost::thread mit moveable Objekt on Mon, 30 May 2011 21:29:07 GMT]]></title><description><![CDATA[<p>Das Objekt in nen std::vector speichern und mit std::ref übergeben? Oder verwechsel ich da gerade was?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071216</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 30 May 2011 21:29:07 GMT</pubDate></item><item><title><![CDATA[Reply to boost::thread mit moveable Objekt on Mon, 30 May 2011 21:32:01 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">boost::thread thread(std::bind(counter));
</code></pre>
<p>funktioniert möglicherweise.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071217</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 30 May 2011 21:32:01 GMT</pubDate></item><item><title><![CDATA[Reply to boost::thread mit moveable Objekt on Mon, 30 May 2011 21:42:40 GMT]]></title><description><![CDATA[<p>Du musst schon <code>std::move</code> verwenden, damit der richtige Konstruktor aufgerufen wird. Es wäre mir neu, wenn man benannte Objekte implizit &quot;moven&quot; könnte.<br />
Vielleicht hilft das aber auch nicht, falls <code>boost::thread</code> den Funktor intern kopiert. Müsste man nachlesen oder ausprobieren.</p>
<p>EDIT: Ja, <code>thread</code> <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html#thread.thread_management.thread.callable_constructor" rel="nofollow">darf die Funktion kopieren</a>. Das kann so also nicht funktionieren.</p>
<p>EDIT: Nimm doch einfach einen <code>shared_ptr</code> .</p>
<p>EDIT: Vielleicht ist das mit dem <code>shared_ptr</code> nicht so offensichtlich, also hier ein Beispiel:</p>
<pre><code class="language-cpp">boost::shared_ptr&lt;Counter&gt; c = boost::make_shared&lt;Counter&gt;();
	boost::thread t(boost::bind(&amp;Counter::operator (), c));
</code></pre>
<p>Natürlich solltest du deine <code>thread</code> s nicht in der Schleife auf dem Stack anlegen, oder ist das so beabsichtigt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071218</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Mon, 30 May 2011 21:42:40 GMT</pubDate></item><item><title><![CDATA[Reply to boost::thread mit moveable Objekt on Mon, 30 May 2011 21:55:27 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<p>Du musst schon <code>std::move</code> verwenden, damit der richtige Konstruktor aufgerufen wird. Es wäre mir neu, wenn man benannte Objekte implizit &quot;moven&quot; könnte.</p>
</blockquote>
<pre><code class="language-cpp">boost::thread thread(std::move(counter));
</code></pre>
<p>So funkts...</p>
<p>Kein Kommentar <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>Manchmal vergisst man halt triviale Sachen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /><br />
Naja... Fast 2 Monate kein C++ geproggt :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2071222</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2071222</guid><dc:creator><![CDATA[XSpille]]></dc:creator><pubDate>Mon, 30 May 2011 21:55:27 GMT</pubDate></item></channel></rss>