何时使用锁定指南 [英] Guidelines of when to use locking
问题描述
我想知道是否有任何guidelineswhich开发人员应该遵循何时(何)放置锁
例如:据我所知,代码如这应该被锁定,以避免另一个线程改变SomeHeapValue意外的价值的可能性。
类Foo
{
公共SomeHeapObject myObject的;
公共无效DoSummat(对象inputValue_)
{
myObject.SomeHeapValue = inputValue_;
}
}
我的问题是,然而,如何深做一件与锁去?举例来说,如果我们有这样的代码:
类Foo
{
公共SomeHeapObject myObject的;
公共无效DoSummat(对象inputValue_)
{
myObject.SomeHeapValue = GetSomeHeapValue();
}
}
我们应该锁定在DoSummat( ...)方法,还是应该在GetSomeHeapValue()方法锁定?
是否有你所有strcturing多线程代码时,要记住任何指引<? / p>
用于锁定和线程我找到的最好的指南,就是这个页面(这是锁定和线程工作时,我咨询文本):
http://www.albahari.com/threading/一>
哟希望款锁定和线程安全,但读其余此外,它是写得很好。
I would like to know if there are any guidelineswhich a developer should follow as to when (and where) to place locks.
For instance: I understand that code such as this should be locked, to avoid the possibility of another thread changing the value of SomeHeapValue unexpectedly.
class Foo
{
public SomeHeapObject myObject;
public void DoSummat(object inputValue_)
{
myObject.SomeHeapValue = inputValue_;
}
}
My question is, however, how deep does one go with the locking? For instance, if we have this code:
class Foo
{
public SomeHeapObject myObject;
public void DoSummat(object inputValue_)
{
myObject.SomeHeapValue = GetSomeHeapValue();
}
}
Should we lock in the DoSummat(...) method, or should we lock in the GetSomeHeapValue() method?
Are there any guidelines that you all keep in mind when strcturing multi-threaded code?
The best guide for locking and threading I found, is this page (this is the text I consult when working with locking and threading):
http://www.albahari.com/threading/
Yo want the paragraph "Locking and Thread Safety", but read the rest also, it is very well written.
这篇关于何时使用锁定指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!