Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I can see the thread runs, the signal is intercepted</p> </blockquote> <p>Are you sure? I don't think the signal is event sent.</p> <blockquote> <p>but the posted handler is not executed. What am I doing wrong?</p> </blockquote> <p>Your constructor creates a new thread, then waits for it to finish, so the constructor doesn't return until the new thread exits. That means this line in <code>main</code> won't execute until the receiver thread exits:</p> <pre><code> Package1Signals::getInstance()-&gt;signal1(); </code></pre> <p>Maybe you want the <code>boost::thread</code> to be a member of your class, so it has the same lifetime as your class, instead of being a local variable in the constructor.</p> <p>For style considerations, you don't need to use <code>boost::ref(*this)</code> you can just pass <code>this</code>, and you don't need to use <code>boost:bind</code> to create the thread, read the <a href="http://www.boost.org/doc/libs/1_47_0/doc/html/thread/thread_management.html#thread.thread_management.thread.multiple_argument_constructor" rel="nofollow">docs</a> which tell you that constructing a <code>thread</code> with multiple arguments is equivalent to passing those arguments to <code>bind</code> and constructing the thread with the result i.e. you can just say</p> <pre><code>thread(&amp;Class1::classifierBehavior, this); </code></pre> <p>Which is much simpler and easier to read!</p> <p><strong>Update</strong>: Now you've fixed the constructor so it doesn't block you have a race condition between connecting the receiver to the signal, which happens in the new thread, and publishing the signal, which happens in the main thread, as soon as the constructor finishes. If the new thread takes a few milliseconds to start executing then it will be too late and miss the signal, because in the main thread the constructor will have finished and the signal will have been emitted already.</p> <p>Try connecting the receiver <em>before</em> starting the new thread:</p> <pre><code>Class1::Class1(){ Package1Signals::getInstance()-&gt;signal1.connect(boost::bind(&amp;Class1::Reception1, this,_1)); thread = boost::thread(&amp;Class1::classifierBehavior,this); }; void Class1::classifierBehavior(){ service.run(); }; </code></pre> <p>This way the receiver is connected before the signal is emitted.</p> <p>I <em>think</em> the <code>io_service</code> will still get the event even if the event gets posted before the service is running, but you might want to check that, otherwise there's another race condition there.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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