Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11 rvalue calls destructior twice
    primarykey
    data
    text
    <p>I'm trying to make a class runner (run a class at a fixed time freq), which runs a class in another thread, and can be controlled (like pause, resume, stop) from main thread.</p> <p>So I want to take advantage of C++11's Functor and other features. But I have a strange problem, the Functor's destructor passed into Runner has been called twice.</p> <pre><code>#include &lt;iostream&gt; #include &lt;chrono&gt; #include &lt;thread&gt; using namespace std; class Runner { public: typedef function&lt;bool()&gt; fn_t; Runner(fn_t &amp;&amp;fn) : fn_(move(fn)), thread_(Thread, ref(*this)) { cout &lt;&lt; "Runner" &lt;&lt; endl; } ~Runner() { cout &lt;&lt; "~Runner" &lt;&lt; endl; thread_.join(); } private: fn_t fn_; thread thread_; static void Thread(Runner &amp;runner) { while (runner.fn_()) { cout &lt;&lt; "Running" &lt;&lt; endl; this_thread::sleep_for(chrono::milliumseconds(1)); } } }; class Fn { public: Fn() : count(0) { cout &lt;&lt; "Fn" &lt;&lt; endl; } ~Fn() { cout &lt;&lt; "~Fn" &lt;&lt; endl; } bool operator()() { return (++count &lt; 5); } private: int count; }; int main (int argc, char const* argv[]) { Fn fn; Runner runner(move(fn)); return 0; } </code></pre> <p>outpus:</p> <pre><code>Fn Runner ~Fn ~Runner Running Running Running Running Running ~Fn ~Fn </code></pre> <p>and if I change </p> <pre><code>Fn fn; Runner runner(move(fn)); </code></pre> <p>to </p> <pre><code>Runner runner(Fn()); </code></pre> <p>the program outpus nothing and stalls. I have tried to disable compiling optimization, nothing changes. Any explanation?</p> <p>How can I fix this or do the samething in other method? Should I implement this class like std::async / std::thread?</p> <p><strong>Update to</strong> <code>Runner runner(Fn())</code></p> <p>This statement was interrupted as a function declaration.</p> <p><code>Runner runner((Fn()))</code> solved problem.</p> <p>Thanks for all comments and answers. After look into <a href="http://thbecker.net/articles/rvalue_references/" rel="nofollow">rvalue</a>, seems I have misunderstand the meaning of rvalue reference from ground 0. I will try some other ways.</p> <p><strong>Final Solution for this problem</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;chrono&gt; #include &lt;thread&gt; #include &lt;vector&gt; using namespace std; template&lt;typename T, typename... Args&gt; class Runner { public: Runner(Args&amp;&amp;... args) : t(forward&lt;Args&gt;(args)...), thread_(Thread, ref(*this)) { cout &lt;&lt; "Runner" &lt;&lt; endl; } ~Runner() { cout &lt;&lt; "~Runner" &lt;&lt; endl; thread_.join(); } private: T t; thread thread_; static void Thread(Runner &amp;runner) { while (runner.t()) { cout &lt;&lt; "Running" &lt;&lt; endl; this_thread::sleep_for(chrono::milliseconds(100)); } } }; class Fn { public: Fn() : count(0) { cout &lt;&lt; "Fn" &lt;&lt; endl; } ~Fn() { cout &lt;&lt; "~Fn" &lt;&lt; endl; } bool operator()() { return (count++ &lt; 5); } private: int count; }; int main (int argc, char const* argv[]) { //vector&lt;Fn&gt; fns; //fns.emplace_back(Fn()); Runner&lt;Fn&gt; runner; return 0; } </code></pre> <p>outpus:</p> <pre><code>Fn Runner ~Runner Running Running Running Running Running ~Fn </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload