Closing socket blocked on read doesn't unblock the waitSet

26 views
Skip to first unread message

Vojtěch Frič

unread,
Aug 7, 2013, 3:36:32 AM8/7/13
to lib...@googlegroups.com
Hi,
thanks to your explanation earlier I've managed to make server working. But I have stumbled upon another problem, with terminating the server.

ClientThread : Thread {
    privateSocket;
    waitSet(1);

    ClientThread(TCPSocket &clientSocket) {
        privateSocket = clientSocket
        waitSet.Add(&privateSocket, waitable::READ);
    }

    void Run() {
         while(true){
             waitSet.Wait();
             if(privateSocket.CanRead()){
                 // read data
                 if(bytesRead == 0)
                      break; // connection closed
             } else {
                 break; //error
             }
         }
    }

   void Terminate(){
         waitSet.Remove(&privateSocket);
         privateSocket.Close();
   }
}


The code above is rough sketch of my program, it's written in pseudo C++ (sorry about that).
The idea is that this thread is blocked on the socket, waiting for read. - green text
The ClientThread::Terminate method is called asynchronously, from signal handler. This handler responds to SIGINT. When I send SIGINT to my program, the Terminate method finishes, but the waitSet doesn't unblock.

I have idea of waiting on waitSet with timeout and then checking some variable, whether the thread should terminate, but that seems too close to active waiting, which I don't want.

The question is, is there any way to make my code work? And if not, how can I gracefully terminate thread blocked on waitSet.Wait()?

Thanks,
vf











 

Ivan Gagis

unread,
Aug 7, 2013, 7:54:28 AM8/7/13
to lib...@googlegroups.com
Hi,

yes, there is a way to do it gracefully.
I see you are closing the socket from signal handler... maybe it is ok, but I would better close it from the thread which holds the socket, so, all the socket operations would be performed from the same thread. And also removing socket from waitset should be done from the same thread to avoid possible undefined behaviour, since waitset methods are not thread safe.

How to achieve that.. You need to communicate with the thread somehow, send messages to it. Thus, the thread will handle the messages and do the actions needed. Have a look at ting::mt::Queue and ting:mt::Message classes. The ting::mt::Queue class extends ting::Waitable, so it can be added to waitset (along with your socket) and you can wait for new messages to come to the queue simultaneously with waiting for data to read from socket.

Also, have a look at this WiKi page: http://code.google.com/p/ting/wiki/tingUsageTutorial
it has some tutorials about Queue and Message and how to ask a thread to terminate itself.

If you have more questions, please ask.

Thanks,
Ivan



2013/8/7 Vojtěch Frič <sut...@gmail.com>












 

--
You received this message because you are subscribed to the Google Groups "libting" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libting+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages