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=0.0 required=5.0 tests=none 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 3414BBC6B for ; Thu, 20 Sep 2007 08:10:41 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJir8UbAXQInlGdsb2JhbACOEQEBAQEHBAYHmFo X-IronPort-AV: E=Sophos;i="4.20,276,1186351200"; d="scan'208";a="16455986" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 20 Sep 2007 08:12:01 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l8K6BILL001696 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 20 Sep 2007 08:11:18 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAA+s8Uba1uFinmdsb2JhbACOEQEBAQEHBAYFIpg6 X-IronPort-AV: E=Sophos;i="4.20,276,1186351200"; d="scan'208";a="1075525" Received: from smtp.syd.people.net.au ([218.214.225.98]) by mail1-smtp-roc.national.inria.fr with SMTP; 20 Sep 2007 08:11:59 +0200 Received: (qmail 3545 invoked from network); 20 Sep 2007 06:12:06 -0000 Received: from unknown (HELO hendrix.mega-nerd.net) (218.214.64.136) by smtp.syd.people.net.au with SMTP; 20 Sep 2007 06:12:06 -0000 Received: from hendrix (hendrix [192.168.200.99]) by hendrix.mega-nerd.net (Postfix) with SMTP id 58ECE2FCF8 for ; Thu, 20 Sep 2007 16:11:56 +1000 (EST) Date: Thu, 20 Sep 2007 16:11:56 +1000 From: Erik de Castro Lopo To: caml-list@inria.fr Subject: Re: [Caml-list] Can coThreads be used for message passing architecture? Message-Id: <20070920161156.fce412f3.mle+ocaml@mega-nerd.com> In-Reply-To: <200709200516.04746.jon@ffconsultancy.com> References: <87lkb5fe3f.fsf@pps.jussieu.fr> <87ps0e9b1m.fsf_-_@fel.cvut.cz> <20070919211346.02e1a947.mle+ocaml@mega-nerd.com> <200709200516.04746.jon@ffconsultancy.com> Reply-To: caml-list@inria.fr Organization: Erik Conspiracy Secret Labs X-Mailer: Sylpheed 2.3.1 (GTK+ 2.10.11; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 46F20F06.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 unmanageable:01 spawned:01 overload:01 2.5:98 Jon Harrop wrote: > Carry on... :-) > > Any examples? 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 *) 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. Erik -- ----------------------------------------------------------------- Erik de Castro Lopo ----------------------------------------------------------------- "I believe C++ instills fear in programmers, fear that the interaction of some details causes unpredictable results. Its unmanageable complexity has spawned more fear-preventing tools than any other language, but the solution _should_ have been to create and use a language that does not overload the whole goddamn human brain with irrelevant details." -- Erik Naggum