socket descriptors shared between threads
Kurt Granroth
plug-discuss@lists.plug.phoenix.az.us
Fri, 28 Mar 2003 14:11:18 -0700
Random programming question: How are socket descriptors shared between threads
in Linux?
Say you have the following pseudo-code:
void thread(socket *sock)
{
select( sock );
printf("select exited\n");
}
int main()
{
socket sock = socket_create_and_bind_and_listen();
pthread_create( thread, sock );
close( sock );
}
So we are sharing the same socket descriptor between the main thread and the
worker thread. 'select' blocks on the descriptor. Since this is the same
process, I would expect that the two threads really are sharing the
descriptor and there isn't any copying going on.
HOWEVER, doing the close() on the socket does *not* actually close the socket
descriptor in the worker thread. That is, select() will keep waiting on the
socket as if it was still valid.
Does anybody here know why? What's the reasoning behind that and who made
that decision?
Kurt Granroth