如何正确地锁定值类型? [英] How to properly lock a value type?
本文介绍了如何正确地锁定值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在阅读有关线程和有关锁定。通常的做法是,你不能(也应该)锁定值类型。
I was reading about threading and about locking. It is common practise that you can't (well should not) lock a value type.
所以,问题是,什么是锁定值类型的推荐的方法?我知道有几个方法去这样做的一件事,但我还没有看到一个例子。虽然是在MSDN论坛一个很好的线程,但我似乎无法找到了。
So the question is, what is the recommended way of locking a value type? I know there's a few ways to go about doing one thing but I haven't seen an example. Although there was a good thread on MSDN forums but I can't seem to find that now.
感谢
推荐答案
使用该锁的另一个对象。
Use another object for the lock.
int valueType;
object valueTypeLock = new object();
void Foo()
{
lock (valueTypeLock)
{
valueType = 0;
}
}
这篇关于如何正确地锁定值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文