Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost::Thread access violation when calling join
    text
    copied!<p>I'm having some problems attempting to create a multi-threaded Server. Everything works fine until I need to remove a client from the server. </p> <p>The server is run in it's own thread, then each client has it's own thread as well.</p> <p>I'm using boost::thread for all the threads. When I need to stop the client I call </p> <pre><code>void StopClient() { assert(mThread); mStopMutex.lock(); mStopRequested = true; mStopMutex.unlock(); shutdown(mSocket,2); mThread-&gt;join(); } </code></pre> <p>Adding a breakpoint to the line</p> <pre><code>shutdown(mSocket,2); </code></pre> <p>I can see that mThread doesn't exist! Does this mean that the thread has already exited? Do you always need to call join() for a boost::thread?</p> <p>If I allow the code to run I get an access violation error.</p> <p><strong>Update</strong></p> <p>ServerThread</p> <pre><code>void StartServer() { assert(!mListenThread); mListenThread = boost::shared_ptr&lt;boost::thread&gt;(new boost::thread(boost::bind(&amp;ServerThread::Listen, this))); mUpdateThread = boost::shared_ptr&lt;boost::thread&gt;(new boost::thread(boost::bind(&amp;ServerThread::Update, this))); } void StopServer() { assert(mListenThread); mStopRequested = true; mMutex.lock(); for(int i = 0; i &lt; mClients.size(); i++ ) mClients[i]-&gt;StopClient(); mMutex.unlock(); mListenThread-&gt;join(); } void Listen() { while (!mStopRequested) { std::cout &lt;&lt; "Waiting for connection" &lt;&lt; std::endl; if(mClientSocket = accept( mServerSocket, (sockaddr*) &amp;mServerAddr, &amp;addrlen ) ) { mMutex.lock(); if( mClients.size() &gt; 0 ) { for( int i = 0; i &lt; mClients.size(); i++ ) { if( mClients[i]-&gt;getClientSocket() != mClientSocket ) { ClientThread newClient; newClient.Initialise(mClientSocket); mClients.push_back(&amp;newClient); mClients[mClients.size()-1]-&gt;StartClient(); break; } } } else { ClientThread newClient; newClient.Initialise(mClientSocket); mClients.push_back(&amp;newClient); mClients[mClients.size()-1]-&gt;StartClient(); } mMutex.unlock(); } } } void Update() { while (!mStopRequested) { mMutex.lock(); std::cout &lt;&lt; "::::Server is updating!::::" &lt;&lt; mClients.size() &lt;&lt; std::endl; for( int i = 0; i&lt; mClients.size(); i++ ) { if( !mClients[i]-&gt;IsActive() ) { mClients[i]-&gt;StopClient(); mClients.erase( mClients.begin() + i ); } } mMutex.unlock(); } } </code></pre> <p>ClientThread</p> <pre><code>void StartClient() { assert(!mThread); mThread = boost::shared_ptr&lt;boost::thread&gt;(new boost::thread(boost::bind(&amp;ClientThread::Update, this))); } void Update() { bool stopRequested; do { mStopMutex.lock(); stopRequested = mStopRequested; mStopMutex.unlock(); std::cout &lt;&lt; "lol" &lt;&lt; std::endl; if( mTimeOut &lt; 1000 ) { mTimeOut++; } else { mActive = false; } boost::this_thread::interruption_point(); } while( !stopRequested); } </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