From: Philippe Wang <philippe.wang.lists@gmail.com>
To: ygrek <ygrekheretix@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] threads
Date: Wed, 9 Sep 2009 01:20:06 +0200 [thread overview]
Message-ID: <4d1b2df20909081620r55accbc1q837dd78317a7923f@mail.gmail.com> (raw)
In-Reply-To: <20090908203319.cf4b1ff6.ygrekheretix@gmail.com>
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 <ygrekheretix@gmail.com> 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
next prev parent reply other threads:[~2009-09-08 23:20 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-08 17:33 threads ygrek
2009-09-08 23:20 ` Philippe Wang [this message]
2009-09-10 18:17 ` [Caml-list] threads ygrek
-- strict thread matches above, loose matches on Subject: below --
2005-06-10 17:56 Threads Jonathan Bryant
2005-06-11 7:46 ` [Caml-list] Threads Christophe Raffalli
2005-06-11 14:02 ` james woodyatt
2005-06-11 21:03 ` Christophe Raffalli
2002-03-20 2:59 [Caml-list] threads Francois Rouaix
2002-03-20 8:11 ` Remi VANICAT
2002-03-20 10:54 ` Sylvain LE GALL
2002-03-22 14:55 ` Xavier Leroy
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4d1b2df20909081620r55accbc1q837dd78317a7923f@mail.gmail.com \
--to=philippe.wang.lists@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=ygrekheretix@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox