module Var:sig
..end
Global variables with mutex.
In a GUI, it is quite likely that a thread has to modify a variable
owned by another thread. This is particularly true in Bogue
(because connections created by Widget.connect
use a new Thread
when executed, unless you specify ~priority:Main
). In order to
protect against concurrent access to a shared variable, one should
use a special kind of variable. This is the goal of this module.
Warning: working with threads is subtle, and using Var
will
not magically make all problems disappear. In particular if two
variables from two different threads want to access each other, you
can end up into a stall, and freeze your program. This can happen
more often that one thinks, because a Var may contain a Layout
,
and we know that sometimes layouts want to modify themselves...
type 'a
t
val create : 'a -> 'a t
create v
returns a Var with initial value v
.
val get : 'a t -> 'a
val set : 'a t -> 'a -> unit
set v value
waits until no thread is accessing the Var v
and then sets its value to value
.
val with_protect : 'a t -> ('a -> 'b) -> 'b
with_protect v f
applies f
to the value of v
, while protecting v
from the access of any other thread.
val protect_fn : 'a t -> ('a -> 'b) -> 'b
Var.with_protect
.