muffel
muffel

Reputation: 7370

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).

Upvotes: 2

Views: 386

Answers (2)

7sharp9
7sharp9

Reputation: 2167

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.

Upvotes: 2

Tomas Petricek
Tomas Petricek

Reputation: 243096

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.

Upvotes: 4

Related Questions