Re: simultaneously read and write on one socket file
On Jun 11, 9:43 pm, "cr88192" <cr88...@hotmail.com> wrote:
"Pallav singh" <singh.pal...@gmail.com> wrote in message
Assume a tcp/ip connection between two Linux platforms
established via the socket interface. The processes on the
two platforms communicate through a file descriptor.
Is it possible to simultaneously read and write on one
socket file descriptor from 2 threads within the process,
i.e. one thread reads data the other thread writes data at
the same time on the same file descriptor ?
Or will this screw up my data ?
I guess the topic police wont like this topic...
It's obviously posted to the wrong group.
but, anyways, "it depends", better would be to "play it safe"
and use a mutex.
Which is a nice way to get your process to hang.
FWIW, it is further an issue if one is using blocking IO (vs
blocking IO), as AFAIK Linux's mutex'es are of "agressive"
variety (AKA: whenever competing for a lock, there will be a
spike in CPU load (as one thread tries repeatedly to lock the
mutex, while the other thread sleeps on IO), meaning they are
not good for locks which are likely to be held for a period of
If that's the case, it's time to change systems. There will be
a (very short) spike when the a process attempts to acquire a
locked mutex, and is suspended. After that, both processes
should be sleeping, and taking 0% CPU. (The problem, of course,
is that both are sleeping, so nothing happens.)
in this case (I forget the details as to how to pull it off on
Linux), it is possible to get the waiting thread to sleep and
then wake it back up again once notified (this is more how the
WIN32 API's "mutex objects" work AFAIK, but it is not free as
locking/unlocking is slow), thus no 100% CPU load as the other
thread waits for the mutex to release...
It's possible to do what he want from a single thread, with no
locks, by polling on input: the process puts a relatively short
timeout on the input, and when woken up because of the time out,
checks to see if there is anything to send, and sends it. Using
two separate threads is cleaner.
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34