Note that there are some explanatory texts on larger screens.

plurals
  1. POboost: thread not executing an handler posted after the reception of a signal
    text
    copied!<p>I am getting acquainted with boost thread and signals. I am thus implementing this simple example, I only post the cpp file of an example class implementing a thread capable of executing a method when a Signal1 is fired. The signal is defined within a Package1Signals singleton (excuse me for these names, they have been generated from a model)</p> <p>Class1.hpp</p> <pre><code>#ifndef CLASS1_HEADER #define CLASS1_HEADER #include &lt;boost/asio/io_service.hpp&gt; #include &lt;boost/asio/ip/udp.hpp&gt; #include &lt;boost/asio/signal_set.hpp&gt; #include "Package1.hpp" #include &lt;boost/thread.hpp&gt; class Class1{ private: boost::asio::io_service service; public: boost::thread thread; Class1(); void classifierBehavior(); void Reception1(Signal1 signal1); void Reception1Behavior(); }; #endif </code></pre> <p>Class1.cpp</p> <pre><code>#include "Class1.hpp" #include &lt;boost/thread.hpp&gt; #include &lt;boost/date_time.hpp&gt; #include "Package1.hpp" #include "Package2.hpp" #include "Package1.hpp" Class1::Class1(){ //boost::thread thread(boost::bind(&amp;Class1::classifierBehavior,boost::ref(*this))); //thread.join(); thread = boost::thread(&amp;Class1::classifierBehavior,this); }; void Class1::classifierBehavior(){ Package1Signals::getInstance()-&gt;signal1.connect(boost::bind(&amp;Class1::Reception1, this,_1)); service.run(); }; void Class1::Reception1(Signal1 signal1){ std::cout&lt;&lt;"Signal received\n"; service.post(boost::bind(&amp;Class1::Reception1Behavior, this)); } void Class1::Reception1Behavior(){ std::cout&lt;&lt;"Behavior executed\n"; } </code></pre> <p>Package1.hpp</p> <pre><code>#ifndef PACKAGE1_HEADER #define PACKAGE1_HEADER #include &lt;boost/signal.hpp&gt; struct Signal1{ }; class Package1Signals{ private: Package1Signals(); static Package1Signals * instance; public: boost::signal&lt;void(Signal1)&gt; signal1; static Package1Signals * getInstance(); }; #endif </code></pre> <p>Package1.cpp</p> <pre><code> #include "Package1.hpp" Package1Signals * Package1Signals::instance = NULL; Package1Signals::Package1Signals(){} Package1Signals * Package1Signals::getInstance(){ if(!instance){ instance = new Package1Signals(); } return instance; } </code></pre> <p>here is the code which executes it</p> <pre><code>int main() { Class1 test; Package1Signals::getInstance()-&gt;signal1(); test.thread.join(); int k =0; std::cin&gt;&gt;k; return 0; } </code></pre> <p>I can see the thread runs, the signal is intercepted but the posted handler is not executed. What am I doing wrong? </p>
 

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