How can a cacheline sized struct wrapping a mutable, volatile int64 be created in F#? -
i want create cacheline sized struct wrapping mutable, volatile int64 in f#. i’ve tried various struct definitions including 1 below, can't compile.
[<struct; structlayout(layoutkind.explicit, size = 64)>] type mystruct1 (initval:int64) = [<volatilefield>] let mutable value = initval member x.value () = value , set(valin) = value <- valin
which gives error: "structs cannot contain value definitions because default constructor structs not execute these bindings. consider adding additional arguments primary constructor type". can't see additional arguements add primary constructor above.
any ideas?
the struct
definition be
[<struct; structlayout(layoutkind.explicit, size = 64)>] type mystruct = [<fieldoffset(0)>] val mutable value : int64 new(initval:int64) = { value = initval } member x.value get() = x.value , set(valin) = x.value <- valin
but then, [<volatilefield>]
not allowed on val bindings , structs can't contain let bindings.
tl;dr: afaik this impossible in f#
as pointed out @v.b. use interlocked
gives superset of volatile
s guarantees (stronger guarantees ≈ more overhead). might better private
ize value
prevent (accidental) writes circumventing barrier:
[<struct; structlayout(layoutkind.explicit, size = 64)>] type mystruct = [<fieldoffset(0)>] val mutable private value : int64 new(initval:int64) = { value = initval } member public x.value get() = interlocked.read(&x.value) , set(valin) = interlocked.exchange(&x.value, valin) |> ignore
Comments
Post a Comment