caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ivan Gotovchits <ivg@ieee.org>
To: caml-list@inria.fr
Subject: [Caml-list] [lwt] multiplexing several threads
Date: Thu, 31 Jan 2013 09:37:29 +0400	[thread overview]
Message-ID: <87622dx692.fsf@golf.niidar.ru> (raw)


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?"...

             reply	other threads:[~2013-01-31  5:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-31  5:37 Ivan Gotovchits [this message]
2013-01-31  9:53 ` Mauricio Fernandez
2013-01-31 10:26   ` Mauricio Fernandez

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=87622dx692.fsf@golf.niidar.ru \
    --to=ivg@ieee.org \
    --cc=caml-list@inria.fr \
    /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).