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

RE: sysread blocking writer thread?

Thread Previous
From:
Shmuel Fomberg
Date:
December 25, 2007 09:36
Subject:
RE: sysread blocking writer thread?
Message ID:
000401c8471c$ac05d960$6801a8c0@semuel7
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
myself.

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
read. 
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,
Shmuel.



Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About