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
[Caml-list] More info on the Event module
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-12-01 (23:20)
From: Ker Lutyn <ker527mail@y...>
Subject: Re: [Caml-list] More info on the Event module
My question about the Event module: the "natural" way to use it appears to be
with tons of threads, as in my example below. But using wrap and wrap_abort,
etc, it would be possible to promote all events up to a single manager that
maintains a list of events and does repeated selects. This would save on
threads. Has anyone tried this approach? Is it worth trying?

Here's a simple example of using the Event module. It prints an integer every
0.1 seconds, inverting it every 0.3 seconds.

$ ocamlopt -o test -I +threads unix.cmxa threads.cmxa test.ml


open Event

let (++) x f = f x

let timer seconds o =
  let run () =
    while true do
      Thread.delay seconds; sync (send o ())
  in Thread.create run ()

let ints x i o =
  let run () =
    let rec loop x =
      sync (receive i); sync (send o x); loop (x + 1)
    in loop x
  in Thread.create run ()

let printer n i =
  let run () =
    for x = 1 to n do
      Printf.printf "%d\n" (sync (receive i));
      flush stdout
  in Thread.create run ()

let switch t i o =
  let run () =
    let rec loop invert =
      select [
	wrap (receive t)
	  (fun () ->
	     loop (not invert));
	wrap (receive i)
	  (fun x ->
	     sync (send o (if invert then (- x) else x));
	     loop invert)
      ] in loop false
  in Thread.create run ()

let _ =
  let a = Event.new_channel () in
  let b = Event.new_channel () in
  let c = Event.new_channel () in
  let d = Event.new_channel () in
  timer 0.1 a ++ ignore;
  ints 0 a b ++ ignore;
  timer 0.3 c ++ ignore;
  switch c b d ++ ignore;
  printer 10 d ++ Thread.join

--- Jonathan Roewen <jonathan.roewen@gmail.com> wrote:

> Hi,
> I'm looking for some more info on the Event module. I can't figure it out =/
> Event.sync (Event.send chan 6; Event.receive chan);; blocks forever...
> What I want to do is rewrite some concurrent Haskell code in OCaml
> (concurrent haskell code uses GHC/pre-emptive threading). I'm not sure
> if the Event module is the way to go. Figuring out how to do the
> equivalent of Haskell's channels and mvars is proving difficult =/
> Jonathan
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

Yahoo! DSL – Something to write home about. 
Just $16.99/mo. or less.