简体   繁体   中英

Shared-Exclusive lock implementation for F#

I am currently working on an F# project that contains many parallel calculations. As being bound to the trimmed.Net 4 Silverlight Framework (because of the required Silverlight compatibility) I cannot use the available.Net implmenetations and may only use the Monitor object and simple locking by using the lock Keyword.

Do you have any idea how a Shared-Exclusive lock implementation for F# might be desigend best?

I did some functional programming before but haven't concentrated on doing that parallel stuff (yet).

I'm not quite sure what exactly you need - if you need standard mutual exclusion, then the lock function is available in the Silverlight version of F# runtime.

If you need something more complex (such as multiple readers, single writer), then you can rewrite your code to use F# agents and solve the problem more elegantly. If you can add more details about the higher-level structure of your code, then someone can post an example how to solve your particular problem.

Anyway, the following SO answer shows how to write a reusable agent for multiple readers/single writer:

As mentioned in the comment, you should probably try to avoid writing locks and low-level synchronization primitives explicitly, as this is a source of infinite number of bugs. F# agents give you a higher-level abstraction that is easier to use.

Theres an excellent chapter on this in Expert F# 2.0, Chapter 13 Reactive, Asynchronous, and Parallel Programming.

See example 13.13 shows a nice Request gate, something similar may be of use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM