develooper Front page | perl.ithreads | Postings from December 2007

RE: sysread blocking writer thread?

Thread Previous
Shmuel Fomberg
December 25, 2007 09:36
RE: sysread blocking writer thread?
Message ID:
Hi All.

>I have a program where two threads are handling one socket. One thread is
>reading and the other is writing to the socket.
>(using ActiveState Perl 5.8.8_822 on WinXP)
>However, the writer thread sometimes blocks for no apperent reason. I saw
>that the reader thead is using sysread, and got a suspicion that this
>sysread is blocking the writer thread from writing.
>Is it possible?

I read the archive to this mailing list, and saw a thread where one said
that Socket is not thread safe.
So my conclusion is that some mutex is protecting socket, the read operation
is catching it, and the write is waiting for it. Thus protecting me from

My solution was to replace the function that reads data from the socket in a
new version that peeks (using ioctl) to see how many bytes are available.
sleeps periodically until enough data arrive, and only then does the actual
This way the (supposed) mutex is not being hold most of the time, and the
writer thread is free to write whenever it wants.

Happy Coding,

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About