caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] More info on the Event module
@ 2005-12-01 22:36 Jonathan Roewen
  2005-12-01 23:20 ` Ker Lutyn
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Roewen @ 2005-12-01 22:36 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] More info on the Event module
  2005-12-01 22:36 [Caml-list] More info on the Event module Jonathan Roewen
@ 2005-12-01 23:20 ` Ker Lutyn
  2005-12-02  2:13   ` Jonathan Roewen
  0 siblings, 1 reply; 3+ messages in thread
From: Ker Lutyn @ 2005-12-01 23:20 UTC (permalink / raw)
  To: Jonathan Roewen, caml-list

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

test.ml:

open Event

let (++) x f = f x

let timer seconds o =
  let run () =
    while true do
      Thread.delay seconds; sync (send o ())
    done
  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
    done
  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. 
dsl.yahoo.com 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] More info on the Event module
  2005-12-01 23:20 ` Ker Lutyn
@ 2005-12-02  2:13   ` Jonathan Roewen
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Roewen @ 2005-12-02  2:13 UTC (permalink / raw)
  To: Ker Lutyn; +Cc: caml-list

Ooh, have to sync the send call! That clears it up =D

Jonathan


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-12-02  2:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-01 22:36 [Caml-list] More info on the Event module Jonathan Roewen
2005-12-01 23:20 ` Ker Lutyn
2005-12-02  2:13   ` Jonathan Roewen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).