If you are using GTK and glib and you are talking about a socket, I would use g_io_add_watch() for this. You do not have to thread it. io_watch accepts a socket descriptor and a callback. You put it inside your gtk_main_loop and when there is data waiting to be read on the socket (ie, an incoming call) the callback function will be called. This is somewhat simpler and cleaner than a separate dedicated thread (altho it amounts to the same thing and I would imagine is implemented that way).
Originally Posted by Whilst
Here's an additional tip: the third parameter to g_io_add_watch_full is a condition. You want G_IO_IN, that is, a normal incoming message. However, it is a good idea to set up a second watch on the same socket with G_IO_HUP, then set the priority of this watch (second parameter) HIGHER** than the IN watch. HUP will respond if the other end of the socket disconnects -- this is an error situation*. Otherwise, if the connection goes bad, you will get an undesirable event on the IN watch (this will be true for a normal socket setup too, except AFAICT the normal socket API does not allow you to detect HUP, so you are stuck with the "undesirable event", which is a non-stop flood of recv's with no content).
* probably the end of the program unless you set up a means of reconnecting.
** lower number = higher priority