caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mauricio Fernandez <mfp@acm.org>
To: caml-list@inria.fr
Subject: Re: [Caml-list] [lwt] multiplexing several threads
Date: Thu, 31 Jan 2013 11:26:45 +0100	[thread overview]
Message-ID: <20130131102645.GB3017@NANA.localdomain> (raw)
In-Reply-To: <20130131095331.GA3017@NANA.localdomain>

On Thu, Jan 31, 2013 at 10:53:31AM +0100, Mauricio Fernandez wrote:
> On Thu, Jan 31, 2013 at 09:37:29AM +0400, Ivan Gotovchits wrote:
> > 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!
> 

Just remembered that you might want to cancel the other thread before
returning too:

> let process s = function
>     Exit -> return (`Exit s)
>   | cmd -> let s = ... in return (`State s)
> 
> let rec loop state user_cmd event =
>   match_lwt
>     Lwt.choose
>       [ (lwt x = user_cmd in return (`User x));
>         (lwt x = event in return (`Event x));
>     ]
>   with
>     `User cmd -> begin match_lwt process state cmd with
>                    `Exit s -> return s

                      ===================

                      `Exit s -> Lwt.cancel event; return s

>                  | `State s -> loop s (read_from_user ()) event
>                  end
>   | `Event cmd -> begin match_lwt process state cmd with
>                    `Exit s -> return s
                                Lwt.cancel user_cmd; return s

>                  | `State s -> loop s user_cmd (read_event ())
>                  end
-- 
Mauricio Fernandez

      reply	other threads:[~2013-01-31 10:26 UTC|newest]

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

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=20130131102645.GB3017@NANA.localdomain \
    --to=mfp@acm.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).