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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 5F4A1820A1 for ; Fri, 16 Aug 2013 10:46:52 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of edwin+ml-ocaml@etorok.net) identity=pra; client-ip=176.9.138.55; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="edwin+ml-ocaml@etorok.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of edwin+ml-ocaml@etorok.net designates 176.9.138.55 as permitted sender) identity=mailfrom; client-ip=176.9.138.55; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="edwin+ml-ocaml@etorok.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of postmaster@mail.etorok.net designates 176.9.138.55 as permitted sender) identity=helo; client-ip=176.9.138.55; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="edwin+ml-ocaml@etorok.net"; x-sender="postmaster@mail.etorok.net"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AjYFAKDmDVKwCYo3/2dsb2JhbABbgwY1iUi2JYEnFnSCJAEBBAEnGQEBNgIPCxgJFg8JAwIBAgFFEwgCiAYKqBKERwEFjVAGkFcWg3yJKoZvgS6GIIEthHyLLIMfgWUk X-IPAS-Result: AjYFAKDmDVKwCYo3/2dsb2JhbABbgwY1iUi2JYEnFnSCJAEBBAEnGQEBNgIPCxgJFg8JAwIBAgFFEwgCiAYKqBKERwEFjVAGkFcWg3yJKoZvgS6GIIEthHyLLIMfgWUk X-IronPort-AV: E=Sophos;i="4.89,893,1367964000"; d="ml'?scan'208";a="29491637" Received: from mail.etorok.net ([176.9.138.55]) by mail2-smtp-roc.national.inria.fr with ESMTP; 16 Aug 2013 10:46:51 +0200 Received: from [172.30.42.25] (unknown [79.114.60.11]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.etorok.net (Postfix) with ESMTPSA id 2E05F46D8 for ; Fri, 16 Aug 2013 10:46:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=etorok.net; s=mailout; t=1376642810; bh=YzV/+gEsmPYsqcyxiUOaRvSeMgKL7bUyLHfyK9J6ozk=; l=2856; h=Date:From:To:References:In-Reply-To:From; b=rKt8BWy8l4qTVOasClsEoHZjqBWCziT6Pj+A+o9vYWInS+Ru005W/mro/07UQB8Q9 E7TlQC9dqJQeVRsxolA3tidsrtAh0evHxyyUb/JAg1uFio9ch228cKwcmaKSse+qi1 wzTUNIus6T5PkkSgfY/moTqtCjOzrHf35OdYByB8= Message-ID: <520DE6F8.9000105@etorok.net> Date: Fri, 16 Aug 2013 11:46:48 +0300 From: =?ISO-8859-1?Q?T=F6r=F6k_Edwin?= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130630 Icedove/17.0.7 MIME-Version: 1.0 To: caml-list@inria.fr References: In-Reply-To: Content-Type: multipart/mixed; boundary="------------090109060707050804070705" X-Virus-Scanned: clamav-milter 0.97.8 at mail X-Virus-Status: Clean Subject: Re: [Caml-list] Threads and "transaction isolation" in OCaml This is a multi-part message in MIME format. --------------090109060707050804070705 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 08/16/2013 12:57 AM, Markus Mottl wrote: > Hi, > > I just wondered how much we can rely on current OCaml-semantics where > context-switches are impossible as long as there are no allocations. I wrote a little test program and this is not true for bytecode -vmthread, but seems to work for bytecode -thread and native -thread: $ ocamlfind ocamlc race.ml -package threads -vmthread -linkpkg -o race && ./race started Check OK worker 2 OK worker 3 OK worker 4 OK worker 5 OK worker 7 OK worker 8 OK worker 9 OK worker 10 OK worker 11 OK worker 12 OK worker 13 OK worker 14 OK worker 0 OK worker 1 OK worker 6 OK worker 15 OK Array sum inconsistent: -33 > > Anyway, is it considered reasonably future-safe to write code of that > sort? I'd rather not have new compiler optimizations, etc., interfere > with these assumptions in the near future. I'd consider this very fragile as you'd need to know the implementation details of every function you call (or only calling your own safe functions). I was surprised to find out that even Array.fold_left creates a temporary ref for example. Best regards, --Edwin --------------090109060707050804070705 Content-Type: text/x-ocaml; name="race.ml" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="race.ml" let n = 10000000 let global = Array.init n (fun i -> 0) let cond = Condition.create () let mutex = Mutex.create () let rec check_consistent accum i = if i >= 0 then check_consistent (accum + global.(i)) (i-1) else if accum <> 0 then begin Printf.eprintf "Array sum inconsistent: %d\n" accum; flush stderr; exit 1 end let rec update_array x i = if i < n then begin if i mod 2 = 0 then global.(i) <- x else global.(i) <- -x; update_array x (i+1) end let start = ref false let barrier () = (* start all threads at same time *) Mutex.lock mutex; while !start = false do Condition.wait cond mutex done; Mutex.unlock mutex let worker i = barrier (); while true; do (* the transaction *) update_array i 0; (* end of transaction *) Printf.printf "worker %d OK\n" i; flush stdout; done let verifier () = barrier (); while true; do check_consistent 0 (n-1); Printf.printf "Check OK\n"; flush stdout; done let () = let a = Array.init 16 (fun i -> Thread.create worker i) in let t = Thread.create verifier () in start := true; Condition.broadcast cond; Printf.eprintf "started\n"; flush stderr; Array.iter Thread.join a; Thread.join t --------------090109060707050804070705--