为了同步对我的属性的访问,我使用 ReaderWriterLockSlim类(class)。我使用以下代码以线程安全的方式访问我的属性。
public class SomeClass
{
public readonly ReaderWriterLockSlim SyncObj = new ReaderWriterLockSlim();
public string AProperty
{
get
{
if (SyncObj.IsReadLockHeld)
return ComplexGetterMethod();
SyncObj.EnterReadLock();
try
{
return ComplexGetterMethod();
}
finally
{
SyncObj.ExitReadLock();
}
}
set
{
if (SyncObj.IsWriteLockHeld)
ComplexSetterMethod(value);
else
{
SyncObj.EnterWriteLock();
ComplexSetterMethod(value);
SyncObj.ExitWriteLock();
}
}
}
// more properties here ...
private string ComplexGetterMethod()
{
// This method is not thread-safe and reads
// multiple values, calculates stuff, ect.
}
private void ComplexSetterMethod(string newValue)
{
// This method is not thread-safe and reads
// and writes multiple values.
}
}
// =====================================
public static SomeClass AClass = new SomeClass();
public void SomeMultiThreadFunction()
{
...
// access with locking from within the setter
AClass.AProperty = "new value";
...
// locking from outside of the class to increase performance
AClass.SyncObj.EnterWriteLock();
AClass.AProperty = "new value 2";
AClass.AnotherProperty = "...";
...
AClass.SyncObj.ExitWriteLock();
...
}
为了避免每当我获取或设置多个属性时不必要的锁定,一旦我发布了 ReaderWriterLockSlim-Object 并在每次我将要获取或设置一堆时从类外部锁定它特性。为此,我的 getter 和 setter 方法使用 ReaderWriterLockSlim 的 IsReadLockHeld 属性和 IsWriteLockHeld 属性检查是否已获取锁。这工作正常并且提高了我的代码的性能。
到目前为止一切顺利,但是当我重新阅读有关 IsReadLockHeld 和 IsWriteLockHeld 的文档时,我注意到了 Microsoft 的评论:
This property is intended for use in asserts or for other debugging purposes. Do not use it to control the flow of program execution.
我的问题是:为什么我不应该为此目的使用 IsReadLockHeld/IsWriteLockHeld?我的代码有什么问题吗?一切都按预期工作,并且比使用递归锁 (LockRecursionPolicy.SupportsRecursion) 快得多。
澄清一下:这是一个最小的例子。我不想知道锁本身是否必要,或者是否可以删除或以不同的方式实现。我只想知道为什么我不应该使用 IsReadLockHeld/IsWriteLockHeld 来控制文档中所述的程序流。
最佳答案
经过进一步研究后,我在 German Support Forum of the Microsoft Developer Network 上发布了同样的问题并与非常有帮助的主持人 Marcel Roma 进行了讨论。他能够联系到 ReaderWriterLockSlim Joe Duffy 的程序员,他写了这个答案:
I'm afraid my answer may leave something to be desired.
The property works fine and as documented. The guidance really is just because conditional acquisition and release of locks tends to be buggy and error-prone in practice, particularly with exceptions thrown into the mix.
It's typically a good idea to structure your code so that you either use recursive acquires, or you don't, (and of course the latter is always easier to reason about); using properties like IsReadLockHeld lands you somewhere in the middle.
I was one of the primary designers of RWLS and I have to admit it has way too many bells and whistles. I don't necessarily regret adding IsReadLockHeld -- as it can come in handy for debugging and assertions -- however as soon as we added it, Pandora's box was opened, and we RWLS was instantly opened up to this kind of usage.
I'm not surprised that people want to use it as shown in the StackOverflow thread, and I'm sure there are some legitimate scenarios where it works better than the alternatives. I merely advise erring on the side of not using it.
总结一下:您可以使用 IsReadLockHeld 和 IsWriteLockHeld 属性有条件地获取锁,一切都会正常,但是这是一种糟糕的编程风格,应该避免使用它。最好坚持递归或非递归锁。为了保持良好的编码风格,IsReadLockHeld 和 IsWriteLockHeld 应该仅用于调试目的。
我想再次感谢 Marcel Roma 和 Joe Duffy 的宝贵帮助。
关于c# - 微软对ReaderWriterLockSlim.IsReadLockHeld/IsWriteLockHeld的评论及其后果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17047122/
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
这是一个基本问题.gemrc文件中是否允许注释?如果是,你会怎么做?我这里查了没用docs.rubygems.org/read/chapter/11 最佳答案 文档说:Theconfigfileitselfisin’’’YAML’’’format.这意味着您可以拥有commentsstartingwith#,例如:#Ilikedocsrdoc:--inline-source--line-numbers 关于ruby-.gemrc评论?,我们在StackOverflow上找到一个类似的问题
我haveaclass它公开了一个字符串值和一个int值(分别是命令输出和退出代码)。除了通过to_s和to_i公开它们之外,我还使用to_str和to_int,如下所示:classStatusdefto_s@outputendalias:to_str:to_sdefto_i@status.exitstatusendalias:to_int:to_iend我的想法是能够在尽可能多的情况下使用这个对象。将其强制转换为字符串或整数会增加可用性。例如,我可以将对象与字符串连接起来:a_string="Outputwas:"+results(我想用这个作为int强制转换的例子,但是Fixnum
我如何做Ruby方法"Flatten"RubyMethod在C#中。此方法将锯齿状数组展平为一维数组。例如:s=[1,2,3]#=>[1,2,3]t=[4,5,6,[7,8]]#=>[4,5,6,[7,8]]a=[s,t,9,10]#=>[[1,2,3],[4,5,6,[7,8]],9,10]a.flatten#=>[1,2,3,4,5,6,7,8,9,10 最佳答案 递归解决方案:IEnumerableFlatten(IEnumerablearray){foreach(variteminarray){if(itemisIEnume
我最近从C#转向了Ruby,我发现自己无法制作可折叠的标记代码区域。我只是想到做这种事情应该没问题:classExamplebegin#agroupofmethodsdefmethod1..enddefmethod2..endenddefmethod3..endend...但是这样做真的可以吗?method1和method2最终与method3是同一种东西吗?还是有一些我还没有见过的用于执行此操作的Ruby惯用语? 最佳答案 正如其他人所说,这不会改变方法定义。但是,如果要标记方法组,为什么不使用Ruby语义来标记它们呢?您可以使用
什么是Linq聚合方法的ruby等价物。它的工作原理是这样的varfactorial=new[]{1,2,3,4,5}.Aggregate((acc,i)=>acc*i);每次将数组序列中的值传递给lambda时,变量acc都会累积。 最佳答案 这在数学以及几乎所有编程语言中通常称为折叠。它是更普遍的变形概念的一个实例。Ruby从Smalltalk中继承了这个特性的名称,它被称为inject:into:(像aCollectioninject:aStartValueinto:aBlock一样使用。)所以,在Ruby中,它称为inj
我是编程新手,正在尝试遵循使用#!用ruby评论。我一直收到消息:bash:matz.rb:找不到命令我正在使用这个评论:#!/usr/bin/envruby我试过有和没有后面的空格!以及有和没有环境。当我使用$哪个rubyruby在:/usr/bin/ruby我还进入了操作系统,将所有用户对文件matz.rb的权限更改为rwx,但没有任何效果。是我做错了什么还是我的系统设置不正确? 最佳答案 /usr/bin/env部分没问题。运行时需要为bash提供matz.rb的路径。如果您在matz.rb所在的目录中,请键入“./matz.