简体   繁体   English

F# 的共享独占锁实现

[英]Shared-Exclusive lock implementation for F#

I am currently working on an F# project that contains many parallel calculations.我目前正在研究一个包含许多并行计算的 F# 项目。 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.由于绑定到 trimmed.Net 4 Silverlight 框架(由于所需的 Silverlight 兼容性),我无法使用可用的.Net 实现,只能使用 Monitor object 和简单的锁定关键字。

Do you have any idea how a Shared-Exclusive lock implementation for F# might be desigend best?您是否知道 F# 的共享独占锁实现可能是最佳设计?

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.我不太确定您到底需要什么 - 如果您需要标准互斥,那么 F# 运行时的 Silverlight 版本中提供了lock function。

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.如果您需要更复杂的东西(例如多个读取器,单个写入器),那么您可以重写代码以使用 F# 代理并更优雅地解决问题。 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:无论如何,以下 SO 答案显示了如何为多个阅读器/单个作者编写可重用代理:

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. F# 代理为您提供更易于使用的更高级别的抽象。

Theres an excellent chapter on this in Expert F# 2.0, Chapter 13 Reactive, Asynchronous, and Parallel Programming. Expert F# 2.0,第 13 章反应式、异步和并行编程中有一个很好的章节。

See example 13.13 shows a nice Request gate, something similar may be of use.参见示例 13.13 显示了一个不错的请求门,类似的东西可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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