Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My guess is that your <code>ConnectionHandler</code> object is being destroyed somewhere. Also, you have defined <code>ConnectionHandler::start</code> in a silly way.</p> <p>First, <code>ConnectionHandler::start</code> should be defined this way:</p> <pre><code>void ConnectionHandler::start(ConnectionHandler * pThis){ pThis-&gt;main(); } </code></pre> <p>The C++11 <code>::std::thread</code> class is perfectly capable of preserving the type of the function argument so there is no need to resort to <code>void *</code>.</p> <p>Secondly, add in this code:</p> <pre><code>void ConnectionHandler::~ConnectionHandler(){ const void * const meptr = this; this-&gt;connectionList_mutex.lock(); ::std::cout &lt;&lt; "ConnectionHandler being destroyed at " &lt;&lt; meptr &lt;&lt; ::std::endl; this-&gt;connectionList_mutex.unlock(); } </code></pre> <p>And change the constructor to read:</p> <pre><code>ConnectionHandler::ConnectionHandler(){ const void * const meptr = this; ::std::cout &lt;&lt; "ConnectionHandler being created at " &lt;&lt; meptr &lt;&lt; ::std::endl; std::thread t(&amp;this-&gt;start, this); t.detach(); } </code></pre> <p>This will show you when the <code>ConnectionHandler</code> object is being destroyed. And my guess is that your code is destroying it while your detached thread is still running.</p> <p>The <code>meptr</code> thing is because <code>operator &lt;&lt;</code> has an overload for <code>void *</code> that prints out the pointer value. Printing out the pointer value for <code>this</code> will allow you to match up calls to the constructor and destructor if you're creating multiple <code>ConnectionHandler</code> objects.</p> <p><em>Edit:</em> Since it turned out I was correct, here is how I would recommend you write the play ConnectionHandler class:</p> <pre><code>#include &lt;iostream&gt; #include &lt;atomic&gt; #include &lt;thread&gt; #include &lt;chrono&gt; #include &lt;mutex&gt; class ConnectionHandler { public: ConnectionHandler(); ~ConnectionHandler(); ConnectionHandler(const ConnectionHandler &amp;) = delete; const ConnectionHandler &amp;operator =(const ConnectionHandler &amp;) = delete; int addNewSocket(); private: int main(); static void start(ConnectionHandler * pThis); ::std::mutex connectionList_mutex; volatile ::std::atomic_bool thread_shutdown; ::std::thread thread; }; ConnectionHandler::ConnectionHandler() : thread_shutdown(false), thread(&amp;this-&gt;start, this) { } ConnectionHandler::~ConnectionHandler() { thread_shutdown.store(true); thread.join(); } void ConnectionHandler::start(ConnectionHandler * pThis){ pThis-&gt;main(); } int ConnectionHandler::addNewSocket(){ ::std::lock_guard&lt; ::std::mutex&gt; lock(connectionList_mutex); ::std::cout &lt;&lt; "test1" &lt;&lt; ::std::endl; return 0; } int ConnectionHandler::main(){ while(!thread_shutdown.load()){ ::std::lock_guard&lt; ::std::mutex&gt; lock(connectionList_mutex); ::std::cout &lt;&lt; "test2" &lt;&lt; ::std::endl; ::std::this_thread::sleep_for(::std::chrono::milliseconds(100)); } return 0; } </code></pre>
 

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