F #: is there a way to mark the contents of the ref cell as volatile?

I was faced with a situation where a ref cell containing a boolean allows me to quite conveniently solve the problem of waiting wait message in order to stop queue waiting. I need to mark this flag as volatile (ie [<VolatileField>] ), but such a field should be mutable according to the compiler, which is exactly what I don't want the link to be - I want its contents to be mutable, not the link cell field itself.

Is there a standard way to tell a ref cell to mark its contents as volatile or do I need to flip my own ref cell (for example, copy-paste the standard definition of a ref cell with [<VolatileField>] pasted just above mutable contents )? Well, correcting this due to the fact that you cannot mark entries as unstable ...

+7
f #
source share
1 answer

Volatile. or Interlocked. methods work fine with ref cells:

 open System.Threading let refCell = ref 0 Volatile.Write(refCell, 42) let fortyTwo = Volatile.Read(refCell) 

You donโ€™t even need & or ! before the variable names ref, they are already referenced. Volatile and Interlocked methods are easier to reason than [<VolatileField>] , as described here .

+7
source share

All Articles