caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ker Lutyn <ker527mail@yahoo.com>
To: Jonathan Roewen <jonathan.roewen@gmail.com>, caml-list@inria.fr
Subject: Re: [Caml-list] More info on the Event module
Date: Thu, 1 Dec 2005 15:20:49 -0800 (PST)	[thread overview]
Message-ID: <20051201232049.35921.qmail@web34612.mail.mud.yahoo.com> (raw)
In-Reply-To: <ad8cfe7e0512011436n2f28ee69hd2f6d41906442f1f@mail.gmail.com>

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 


  reply	other threads:[~2005-12-01 23:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-01 22:36 Jonathan Roewen
2005-12-01 23:20 ` Ker Lutyn [this message]
2005-12-02  2:13   ` Jonathan Roewen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051201232049.35921.qmail@web34612.mail.mud.yahoo.com \
    --to=ker527mail@yahoo.com \
    --cc=caml-list@inria.fr \
    --cc=jonathan.roewen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).