caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] [lwt] multiplexing several threads
@ 2013-01-31  5:37 Ivan Gotovchits
  2013-01-31  9:53 ` Mauricio Fernandez
  0 siblings, 1 reply; 3+ messages in thread
From: Ivan Gotovchits @ 2013-01-31  5:37 UTC (permalink / raw)
  To: caml-list


Dear list,

I'm stuck with a simple problem when using lwt. I have two lwt
threads. One on them reads commands from user (using Lwt_read_line),
another reads events from system. A need to multiplex them. Speaking in
ocaml, I have three functions:

  type state    (* program state to process *)
  type command  (* command to be executed (issued either by user or by
                   system event) *)
  
  val read_from_user: unit -> command Lwt.t
  val read_event: unit -> command Lwt.t
  val process: state -> command -> state Lwt.t

 
And a need to write a function loop

  val loop: state -> state Lwt.t

accepting some state, taking a command from user or an event from system
(whichever comes first) and passing it to `process' function, and doing
it in a cycle, until explicitly ordered to stop (with Exit command). 

The following solution

   let rec loop sc =
     let cmd = read_from_user () <?> read_event () in
     cmd >>= (process state)

   and process sc = function
     | Exit -> return sc
     | cmd  -> (* modify sc*) loop sc

   let r = Lwt_main.run (loop state)

doesn't work when the event thread wakes up first. In that case the next
step of the loop will call Lwt_read_line.read_line before the previous
invocation finishes and it breaks user input.

Please, help me to find a feasible solution to my problem!

-- 
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...

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

end of thread, other threads:[~2013-01-31 10:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-31  5:37 [Caml-list] [lwt] multiplexing several threads Ivan Gotovchits
2013-01-31  9:53 ` Mauricio Fernandez
2013-01-31 10:26   ` Mauricio Fernandez

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).