Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Thread and sockets
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-08-09 (07:57)
From: David Mentre <David.Mentre@i...>
Subject: [Caml-list] Thread and sockets

I'm building a Caml client/server program using posix threads and
sockets. Right now, I have 3 threads:

 1. normal thread for user computation

 2. a thread doing a blocking call to Unix.accept to setup new

 3. a thread doing a blocking call to (with infinite
    timeout) to get messages on opened connections

I've observed that my design is bad and I have several race
conditions. Worse problems are when I open and close connections. For
example, if thread (3) is blocked and a new connection is opened by
thread (2), thread (3) won't read new messages. I have similar problems
when sockets are closed. How to do it properly?

I think I should open a dedicated thread for each new socket [1].
However I don't know how to stop a thread waiting inside a blocking
call. What happens if the remote part close the socket where a thread is
doing a select (or a read). Does this thread get an exception?

I also don't know how to stop cleanly thread (2). Should another thread
close the socket where thread (2) is doing an Unix.accept on?

I would accept with pleasure any advice on this subjet.

Best regards,

[1] At first, I did not choose this design to avoid creating zillions of
    new posix (i.e. kernel, on Linux) threads.
 Opinions expressed here are only mine.
Bug reports:  FAQ:
To unsubscribe, mail  Archives: