From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id E3F357EEAF for ; Thu, 31 Jan 2013 06:37:37 +0100 (CET) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of lambda.q.q@gmail.com) identity=pra; client-ip=209.85.215.47; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="lambda.q.q@gmail.com"; x-sender="lambda.q.q@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of lambda.q.q@gmail.com designates 209.85.215.47 as permitted sender) identity=mailfrom; client-ip=209.85.215.47; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="lambda.q.q@gmail.com"; x-sender="lambda.q.q@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-la0-f47.google.com) identity=helo; client-ip=209.85.215.47; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="lambda.q.q@gmail.com"; x-sender="postmaster@mail-la0-f47.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkMCAF8CClHRVdcvjWdsb2JhbABFvyAWDgEBAQEJCQsJEgYjgkwTBgE5Aw0FJjQBBCABBQFQh2QDDwSgS4Jwjy+EfScNiVABBQyNV4MpA4hbjTKOaj+BWIJG X-IPAS-Result: AkMCAF8CClHRVdcvjWdsb2JhbABFvyAWDgEBAQEJCQsJEgYjgkwTBgE5Aw0FJjQBBCABBQFQh2QDDwSgS4Jwjy+EfScNiVABBQyNV4MpA4hbjTKOaj+BWIJG X-IronPort-AV: E=Sophos;i="4.84,574,1355094000"; d="scan'208";a="572501" Received: from mail-la0-f47.google.com ([209.85.215.47]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 31 Jan 2013 06:36:20 +0100 Received: by mail-la0-f47.google.com with SMTP id fj20so1633265lab.20 for ; Wed, 30 Jan 2013 21:37:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:subject:date:message-id:user-agent :mime-version:content-type; bh=PlNZ/vrYK+4gwY+UWJgzG9XrAmUqAYDrwsUl2J2R63Q=; b=P+NiDMt8Ii/VwLiQaA1ybd1UxPBNaE2JW1G6aoJdWKyAF4KpoIEvDaBqcHuodbDsma kPhGwoeoPlDl/yrlI5RqDs+dxlEWh1igOQzP0qWuHHtZFzS18fSD6kH8PTLyiRPQd3m2 o2MMsTWfVa26oArdGihLUkgZoRPGXMWEVQH1TfNNjU2mdIanKqJBRZX9iam++yjky77N IhWwfd+j9fL2J+765GlE38p0zpdPL1Z2i6lbnkPHxSeF7ksZ8YDgDkowR8hjrpq1CnNF WP0vbpyf0eTnc/j20LH+8e+/C40AK0xaeMZjyuhv8PsBoPKp+dD+fam8sfI8E0cihILT u9UA== X-Received: by 10.152.110.228 with SMTP id id4mr6765171lab.34.1359610656306; Wed, 30 Jan 2013 21:37:36 -0800 (PST) Received: from golf.niidar.ru.niidar.ru ([178.176.59.130]) by mx.google.com with ESMTPS id ie3sm1832272lab.4.2013.01.30.21.37.34 (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Wed, 30 Jan 2013 21:37:35 -0800 (PST) Sender: Ivan Gotovchits From: Ivan Gotovchits To: caml-list@inria.fr Date: Thu, 31 Jan 2013 09:37:29 +0400 Message-ID: <87622dx692.fsf@golf.niidar.ru> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: [Caml-list] [lwt] multiplexing several threads 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?"...