[247] in Pthreads mailing list archive
read() and close()
daemon@ATHENA.MIT.EDU ("John H. Aughey")
Tue Jan 30 19:10:08 1996
To: pthreads@MIT.EDU
Date: Tue, 30 Jan 1996 18:33:04 -0500
From: jha@cs.purdue.edu ("John H. Aughey")
There's a bug (feature?) in the pthreads implementation which causes
close() to block when there is a read() which is blocked already.
Here's some simple code:
#include <stdio.h>
#include <pthread.h>
int fd;
int *func()
{
char buf[2048];
printf("func(): reading from fd\n");
read(fd,buf,2048);
printf("func(): read complete\n");
return NULL;
}
int main()
{
pthread_t t;
fd=0; /* stdin */
pthread_create(&t,NULL,(void *)func,NULL);
sleep(3);
printf("main(): closing fd\n");
close(fd);
printf("main(): close complete\n");
}
If you run this, you'll notice that it blocks during the close. The
expected result was for the descriptor to close and have the read
function fall through (with an error).
In my actual program one thread is blocked reading data from a
descriptor and another external event happens where I want to have that
thread unblock and act accordingly. My only solution (and the solution
which worked under Solaris threads) was to close the file handle. If
closing the descriptor isn't possible, then how could I signal the
blocking thread to indicate that I need it to do something else?
-John