Note that there are some explanatory texts on larger screens.

plurals
  1. POMixing boost::asio and boost::signals2 giving problems
    primarykey
    data
    text
    <p>{boost 1.54}</p> <p>All asio operations are happening on the same io_service which has it's run() called from several std::threads (thus a thread-pool)</p> <pre><code>struct Async : std::enable_shared_from_this&lt;Async&gt; { boost::signals2::signal&lt;void()&gt; m_sig; void read() { auto self = shared_from_this(); boost::asio::async_read_until( /*socket*/, /*bufferToCollectData*/, /*delimiter*/, [self]{self-&gt;onRead(..);} ); } void onRead(..) { m_sig(); } }; struct Process : std::enable_shared_from_this&lt;Process&gt; { std::shared_ptr&lt;Async&gt; m_shrPtrAsync; boost::signals2::connection m_connection; void start() { m_shrPtrAsync = std::make_shared&lt;Async&gt;(); //m_shrPtrAsync-&gt;startUpThings(); auto self = shared_from_this(); m_connection = m_shrPtrAsync-&gt;m_sig.connect( [self]{self-&gt;slot();} ); } void stop() { //this will not delete the slot and have to wait indefinitely until some operation happens on m_sig. //------------------------------- &lt;2&gt; m_connection.disconnect(); //this should force the original slot deletion. m_connection = m_shrPtrAsync-&gt;m_sig.connect([this]{dummy();}); m_connection.disconnect(); } void dummy() {} void slot() { auto self = shared_from_this(); //-------------------- &lt;1&gt; //to not block the calling thread (Async's m_sig()) m_strand.post( [self]{self-&gt;slotPrivateImpl();} ); } void slotPrivateImpl() { /*Processing*/ } }; //This is from the main thread outside the thread-pool {//local scope begins auto shrPtrProcess = std::make_shared&lt;Process&gt;(); shrPtrProcess-&gt;start(); //after sometime shrPtrProcess-&gt;stop(); }//local scope ends. I want shrPtrProcess to delete the contained pointer here //which should in turn delete the Async's pointer in shrPtrProcess-&gt;m_shrPtrAsync </code></pre> <p>Is this safe? When main thread executes <code>shrPtrProcess-&gt;stop();</code> thus deleting the slot from the Async's <code>m_sig</code> and then comes out of the local scope, the last remaining reference to the shared_ptr to <code>Process</code> will have died thus destroying it, but other thread firing <code>m_sig</code> could have entered <code>Process::slot(</code>) by then and about to execute line marked &lt;1> above. Does signals2 guarantee that slots will not be deleted till they are executed completely?</p> <p>If this is not safe then how do i achieve this behaviour of <code>shrPtrProcess</code> destroying <code>Process</code> ptr which in turn destroys <code>Async</code> raw ptr in <code>shrPtrAsync</code> once the local scope ends? If I don't do the hack in place marked &lt;2> above I'll never be able to release resources if m_sig() does not fire anymore as <code>signals2::connection::disconnect()</code> does not cause immediate deletion of slot.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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