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=3.0 required=5.0 tests=AWL,DNS_FROM_RFC_POST, SPF_NEUTRAL 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 F3698BBAF for ; Wed, 9 Sep 2009 01:20:15 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AngBAPeDpkrRVYT5kGdsb2JhbACDA40RgXGIez8BAQEBCQkMBxMDsHGBJysIj14BAwIEgjwIgVAF X-IronPort-AV: E=Sophos;i="4.44,355,1249250400"; d="scan'208";a="46219157" Received: from an-out-0708.google.com ([209.85.132.249]) by mail4-smtp-sop.national.inria.fr with ESMTP; 09 Sep 2009 01:20:07 +0200 Received: by an-out-0708.google.com with SMTP id d14so1620285and.3 for ; Tue, 08 Sep 2009 16:20:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=5G1odcl65CBFi0aHoBBzstr/rDJscZ9lFMUD6jAKrek=; b=R9RtyPdCjo3GHyfpKCFrZwZOc6FcDUKCHK2sAj/rk66OYyFL7HBlijyakvcYSXFcWD Org7nbvpjqNJTMWpfgNaW74fnY4pU4EoldLVYyIvjjtAPsN78/pXFNJRjV9jMzhobwTY +Gmv7aUcTgsDNPik0q9uesi9bApQwZY8bWNyI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=PdjUYxq8NMhrRo+5jr5T4mryeY8Gz2mCldeL3qaRANPHaji2cSiLRLrjE17qzUK2HW 8Fk88dQGF2pp7XXRVy5JDd7WjXKhY1IY5mBd3j38pnMhKUIOJiZsbL/R1LOrLkm25dpy uQPS/Vd98f77o33tXf5MWivanGll762S29A3Y= MIME-Version: 1.0 Received: by 10.101.7.17 with SMTP id k17mr17518796ani.181.1252452006836; Tue, 08 Sep 2009 16:20:06 -0700 (PDT) In-Reply-To: <20090908203319.cf4b1ff6.ygrekheretix@gmail.com> References: <20090908203319.cf4b1ff6.ygrekheretix@gmail.com> Date: Wed, 9 Sep 2009 01:20:06 +0200 Message-ID: <4d1b2df20909081620r55accbc1q837dd78317a7923f@mail.gmail.com> Subject: Re: [Caml-list] threads From: Philippe Wang To: ygrek Cc: caml-list@yquem.inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; scheduler:01 ocaml:01 cheers:01 ocaml:01 ocamlopt:01 beginner's:01 bug:01 2009:98 threads:01 garbage:01 beginners:01 wrote:01 caml-list:01 caml-list:01 functions:01 Hi, > let x = Array.make 100 [] > let update i n = x.(i) <- n :: x.(i) > let read i = x.(i) I don't think you can obtain funny results when you don't put a mutex on these two specific "update" and "read". What is sure is that "update" function is not atomic because you have a value allocation at the right of "<-" (with :: operator), and this may trigger garbage collection and/or make the scheduler change the running thread. What you can be sure with the current official OCaml distribution is that there won't be at the exact same time both an (<-)operation and a (.())operation. But it is actually possible, for instance, for a thread to compute while another one is simultaneously writing on a socket. So it is generally not a good idea to count on some operation atomicity to put or not a mutex lock (well it's good to write some hard-to-debug code)... Cheers, Philippe Wang On Tue, Sep 8, 2009 at 7:33 PM, ygrek wrote: > Hello, > > let x = Array.make 100 [] > let update i n = x.(i) <- n :: x.(i) > let read i = x.(i) > > Consider the following scenario: one thread is `update`ing x, another > thread(s) uses only `read`. Is it safe to use these functions without > locking on mutex? > > I.e. is Array.set atomic? What about updating references (:=) ? > > If I understand correctly these operations require only one cpu > instruction to update one machine word and so should be atomic. Taking > into account "single-cpu affinity" of ocaml program it should be safe > to write such multithreaded code. Is it true? > > Is it safe to assume that ocamlopt won't skip reads/writes to globally > visible memory address using cached value in a register? > > -- > ygrek > http://ygrek.org.ua > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > -- Philippe Wang mail@philippewang.info