English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
threads, signals, and timeout
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-10-27 (10:01)
From: Gabriel Kerneis <kerneis@p...>
Subject: Re: [Caml-list] threads, signals, and timeout
On Mon, Oct 26, 2009 at 12:06:18PM -0700, yoann padioleau wrote:
> I want to write a toy program that uses threads and timeouts in an
> easy way. Those threads may make heavy use of CPU or IO, I don't know.
> I just want an easy API for thread and timeout.
> To be honest, the problem I have to solve is not in OCaml, but I
> thought OCaml programmers would give better answers than C programmers

Looks like CPC[1] is the perfect tool for you: cooperative threads in C,
with the ability to "detach" a thread for CPU intensive operations (i.e.
make it a native preemptive thread), compiled to sequential event-driven
C code (using continuations). In other words, this is some kind of Lwt
for C, with a thread API (instead of the monadic API of Lwt).

But the timeout problem remains: we have a strong feeling that
cooperative threads is the right way to go, and that you should not be
interrupted anywhere (especially in C, where you don't have any GC to
clean-up your malloc'ed variables, file descriptor, etc.). So you have
to be explicit about where the timeouts can happen, e.g. polling
(cooperatively) some condition variable. We are trying to improve the
timeouts handling in CPC at the moment, so we would be glad to get more
details on what you are trying to do exactly, and how we could solve it.

A sample implementation of timeouts can be found in the io.cpc file of
the Hekate BitTorrent seeder[2].

Since this becomes a bit off-topic, I have to mention the fact that the
compiler is written in OCaml (upon the CIL library) ;-)

[1] http://www.pps.jussieu.fr/~jch/software/cpc/
[2] http://www.pps.jussieu.fr/~jch/software/hekate/

NB: please keep in mind that CPC is a research prototype, suitable for
a "toy program" like yours, or even serious ones like Hekate, but it
would not be reasonable to develop industrial software with it at the

Gabriel Kerneis