I find the question hard to understand, so I may have missed a subtlety in the code you have shown.

The purpose of volatile memory is to share memory between the current program and an unknown environment. In your exemple, the value of (quit), a boolean reference, is *not* shared, it is the value of (set_quit) that is passed to the environment. So the question *cannot* be about the accesses to the value "quit", which are clearly marked.

One way to rephrase the question would be: "could the compiler assume than (set_quit) will never be called and thus optimize (!quit) to (false)?". I don't see how that could ever be correct, given that the function (set_quit) is passed to an external function.

On Wed, Nov 4, 2015 at 1:43 PM, Richard W.M. Jones <rich@annexia.org> wrote:
Some code I wrote recently does:

  let quit = ref false in
  let set_quit _ = quit := true in
  Sys.set_signal Sys.sigint (Sys.Signal_handle set_quit);
  Sys.set_signal Sys.sigquit (Sys.Signal_handle set_quit);

and later on (where `tasks' is a list of long-running tasks):

  List.iter (
    fun task ->
      if not !quit then task ();
  ) tasks;

This works fine.  My question is, could a change to the compiler in
future cause the reference to !quit to be optimized away?  And if so,
is there a way to mark it as "volatile" (in the C sense)?

Rich.

--
Richard Jones
Red Hat

--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs