作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
好的,所以我一直在查看 source code for Lazy<T>因为我想扩展它。我知道从理论上讲,它应该是线程安全的,但我不知道它是如何实现的。看起来它是 .value getter,它在读取值之前不会锁定任何东西。
public T Value
{
get
{
Boxed boxed = null;
if (m_boxed != null )
{
// Do a quick check up front for the fast path.
boxed = m_boxed as Boxed;
if (boxed != null)
{
return boxed.m_value;
}
LazyInternalExceptionHolder exc = m_boxed as LazyInternalExceptionHolder;
Contract.Assert(m_boxed != null);
exc.m_edi.Throw();
}
// Fall through to the slow path.
#if !FEATURE_CORECLR
// We call NOCTD to abort attempts by the debugger to funceval this property (e.g. on mouseover)
// (the debugger proxy is the correct way to look at state/value of this object)
Debugger.NotifyOfCrossThreadDependency();
#endif
return LazyInitValue();
}
}
据我了解,为了线程安全,在写入和读取时都必须锁定某些东西,因为如果读取被写入中断,它可能会返回不正确的数据,甚至出现错误。这种理解是正确的,还是 Lazy
我是一名优秀的程序员,十分优秀!