From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=1.0 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id B68B8BC6B for ; Thu, 20 Sep 2007 11:08:13 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAANjU8UbAXQInlGdsb2JhbACOEQEBAQEHBAYH X-IronPort-AV: E=Sophos;i="4.20,277,1186351200"; d="scan'208";a="16462435" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 20 Sep 2007 11:09:35 +0200 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8K98pDY011438 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 20 Sep 2007 11:08:51 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAANjU8UZQW+UCn2dsb2JhbACOEQEBAQEHBAYHIA X-IronPort-AV: E=Sophos;i="4.20,277,1186351200"; d="scan'208";a="16462434" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail4-smtp-sop.national.inria.fr with ESMTP; 20 Sep 2007 11:09:34 +0200 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1IYI0A-0007Ht-Kz for caml-list@inria.fr; Thu, 20 Sep 2007 11:07:14 +0200 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 20 Sep 2007 11:07:14 +0200 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 20 Sep 2007 11:07:14 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: Can coThreads be used for message passing architecture? Date: Thu, 20 Sep 2007 11:06:17 +0200 Message-ID: <871wctk8om.fsf@pps.jussieu.fr> References: <87lkb5fe3f.fsf@pps.jussieu.fr> <87ps0e9b1m.fsf_-_@fel.cvut.cz> <20070919211346.02e1a947.mle+ocaml@mega-nerd.com> <200709200516.04746.jon@ffconsultancy.com> <20070920161156.fce412f3.mle+ocaml@mega-nerd.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:Qq50TWsp5+5lDyZwGf/YEl+zitk= Sender: news X-Miltered: at concorde with ID 46F238A3.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 trivial:01 ocaml:01 ocamlopt:01 cmxa:01 cmxa:01 stdout:01 printf:01 printf:01 endline:01 endline:01 2.5:98 1.5:98 0.3:98 blog:98 Hi, Nice try! Erik de Castro Lopo writes: > All my examples so far are trivial, like the following which creates a > Cothread and then polls for an event sent by the child. > > ------------------------------------------------------------------- > (* > ** Needs Cothreads installed : > ** http://cothreads.sourceforge.net/ > ** > ** Run with : > ** ocaml -I +process unix.cma cothreads.cma poll_event.ml > ** > ** Native compile with : > ** ocamlopt -I +process unix.cmxa cothreads.cmxa poll_event.ml -o poll_event > *) One comment: you don't have to hard-wired "Cothread.xxx" into source, since all the functions you're using are compatible between Thread and Cothread modules, and moreover, coThreads does provide a compatible version of threads.cm(x)a and Thread module being able to work with process. So by "open Cothread" or "open Thread" or "module Thread=Cothread", you'll be given more flexibility to change easily back and forth between coThreads and standard Threads (image sometime later you need to deploy the same code to a standard-OCaml-only environment ...) > let child_thread chan = > (* Print thread id (actually process id) and flush stdout. *) > Printf.printf "child_thread : %d\n%!" (Cothread.id (Cothread.self ())) ; > (* Hang about for a bit. *) > Cothread.delay 2.5 ; > (* Send an event. *) > Event.sync (Event.send chan "Event from child.") ; > (* Hang about a bit more. *) > Cothread.delay 1.5 ; > Cothread.exit () > > let () = > (* Create an event channel for sending stuff between main and child. *) > let chan = Event.new_channel () in > (* Create the child thread and pass it the channel. *) > let t1 = Cothread.create child_thread chan in > (* Set the exit condition to false. *) > let fini = ref false in > while not !fini do > (* Now we can poll on the incoming event. *) > match (Event.poll (Event.receive chan)) with > | None -> Printf.printf "Nothing.\n%!" ; Cothread.delay 0.3 > | Some s -> fini := true ; print_endline s > done ; > (* Wait for child thread to exit. *) > Printf.printf "Waiting for child thread.\n%!" ; > Cothread.join t1 ; > print_endline "Done." > > ------------------------------------------------------------------- > > I intend to blog about this over the weekend. I'll post a URL when > its done. -- Zheng Li http://www.pps.jussieu.fr/~li