开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

C++ preventing EReader reading when socket is closed


 

I have faced a problem with my code for a long time that only occurs during the call to EClientSocket::eDisconnect()

I have a separate message processing thread running which looks like this:
????? ftrMsgProcThrd_ = pool_->submit(
????????? [&]()
????????? {
??????????? while (clientSocket_->isConnected())
??????????? {
????????????? signal_.waitForSignal();?? // This waits 2 seconds.
????????????? reader_->processMsgs();
??????????? }
????????? });

I decided to tackle this annoying bug (not the first time) and have found that after the call to EClientSocket::eDisconnect() which calls EClientSocket::SocketClose() which just calls a Windows Sockets closesocket() on the open socket, I am still getting the message processing thread (EReader thread) trying to perform a Windows Sockets recv() messages on the closed socket resulting in a 509 error. I have traced that error to be socket error 10038 which confirms it is an invalid socket (in this case, a closed socket).

Before the line "reader_->processMsgs()"? I have tried checking for the socket still being open with if(clientSocket_->isConnected()) but it does not solve the problem. The EReader is running in its own thread as per the reader_->start() call.

I thought, perhaps I need to close the EReader before calling eDisconnect so I tried deleting the object and removing the call to eDisconnect() because the destructor of the EReader calls eDisconnect() itself but this does not fix the error. i still get the 509 caused by a read on the closed socket.

I am struggling here and would appreciate advise from any C++ coders that use a multi-threaded approach like the above. It is likely a threading issue but if anyone else has faced a similar "disconnect" issue I would be happy to hear what you did to resolve it.

Join [email protected] to automatically receive all group messages.