Sockets and threads

Kaz Kylheku kaz@ashi.footprints.net
Wed Jan 24 09:17:00 GMT 2001


On Wed, 24 Jan 2001 Neale.Ferguson@softwareAG-usa.com wrote:

> Date: Wed, 24 Jan 2001 09:19:31 +0200
> From: Neale.Ferguson@softwareAG-usa.com
> To: libc-alpha@sources.redhat.com
> Subject: Sockets and threads
>
> I have a sample program which creates a socket, creates a thread, the
> thread issues a blocking accept on the socket, meanwhile the main thread
> (after a delay) closes the socket and waits for the spawned thread to
> complete. On Sun the accept() returns with EBADF but on Linux it doesn't
> return.

Don't do that; use select() or poll() with a reasonably small timeout
instead of the blocking accept. Then have the thread check some
shutdown flag or call pthread_testcancel() or what have you.
You can do this in a loop with fairly fine-grained timeouts for a more
responsive shutdown.

It's a bad idea to blow away sockets that are in use (or any object in
general). It's not a portable shutdown method for blocking socket
calls.

> Here's the Linux log:
>
> initializing...
> creating waiter thread...
> tid: 0
> waiter thread: calling accept()...
> closing socket...
> close: Interrupted system call

So the close never actually happened; it failed with EINTR.



More information about the Libc-alpha mailing list