synchronization - F# lock function with System.Threading.Lock - Stack Overflow

admin2025-04-20  0

.NET 9 introduced a new Lock object (System.Threading.Lock). The C# 13 compiler has also been made able to recognize when the lock keyword is used with such a class instance, producing the correct code.

What about the F# lock function? Does it also automatically recognise the new System.Threading.Lock?

Currently I am using is with a function

let lock9 (lock: System.Threading.Lock) (f: unit -> 't) = 
    let scope = lock.EnterScope()
    try f()
    finally scope.Dispose()

F# use or using cannot be currently used as scope does not implement IDisposable

.NET 9 introduced a new Lock object (System.Threading.Lock). The C# 13 compiler has also been made able to recognize when the lock keyword is used with such a class instance, producing the correct code.

What about the F# lock function? Does it also automatically recognise the new System.Threading.Lock?

Currently I am using is with a function

let lock9 (lock: System.Threading.Lock) (f: unit -> 't) = 
    let scope = lock.EnterScope()
    try f()
    finally scope.Dispose()

F# use or using cannot be currently used as scope does not implement IDisposable

Share Improve this question edited Mar 2 at 22:20 Franco Tiveron asked Mar 2 at 22:10 Franco TiveronFranco Tiveron 2,9361 gold badge27 silver badges44 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

There's an open issue in the F# language repository for this, so I think the answer is currently no. There are some suggestions in the comments for how to add support in your own codebase, if you want to.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745116655a285900.html

最新回复(0)