本文介绍了什么时候是“易变的”用来代替“锁定” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



什么时候使用volatile是合适的关键词?文档只需

状态:


"

volatile修饰符通常用于由
多个线程没有使用lock语句(C#参考)

语句来序列化访问。

"


但是什么时候使用volatile更好?而不是锁定 ?


谢谢,


Sam


---------- --------------------------------------------------

我们正在招聘! B-Line Medical正在寻求.NET

开发人员在医疗产品中的激动人心的职位

开发MD / DC。在轻松的团队环境中使用各种技术

。在Dice.com上查看广告。

解决方案



您还可以使用System.Threading.Interlocked类维护

易失性语义。


Seealso:




对于易失性字段,优化器对内存访问的重新分配是受限制的。

写入易失性字段总是在所有其他字段之后完成内存访问

在指令序列之前。

在所有其他内存访问之前总是从volatile字段读取

发生之后它在指令序列中。


一个易失动的字段作为一种简单的标记方式,记忆操作结束了。


跟随一个例子来自规格:


使用System;

使用System.Threading;

class测试

{

public static int result;

public static volatile bool finished;

static void Thread2(){

result = 143;

finished = true;

}


static void Main(){

finished = false;

//在新线程中运行Thread2()

new Thread(new ThreadStart(Thread2))。Start();

//等待Thread2通过设置
$ b来表示它有结果$ b //结束为真。

for(;;){

if(finished){

Console.WriteLine(" result) = {0}",result);

返回;

}

}

}

}


由于已完成易失性,因此在方法Thread2中,写入结果将始终发生在写入结束之前和方法Main中

完成的读取将始终在读取结果之前发生,因此从

读取导致Main在Thread2中写入之前不会发生。


HTH


Christof


5月21日上午10点35分,Samuel R. Neff< samueln ... @ nomail.comwrote:



通常,如果只有一个线程写入对象(和其他

线程只是读它),你可以使用volatile来逃避。


通常,共享对象需要是一个原子值,所以

读者可能会看到它从A状态到B状态的突然变化,但是它会在A& A之间看不到它。 B.



When is it appropriate to use "volatile" keyword? The docs simply
state:

"
The volatile modifier is usually used for a field that is accessed by
multiple threads without using the lock Statement (C# Reference)
statement to serialize access.
"

But when is it better to use "volatile" instead of "lock" ?

Thanks,

Sam

------------------------------------------------------------
We''re hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

解决方案

You can also the System.Threading.Interlocked class which maintains
volatile semantics.

Seealso: http://www.albahari.com/threading/part4.html


For a volatile field the reodering of the memory access by the optimizer is
restricted.
A write to a volatile field is always done after all other memory accesses
which precede in the instruction sequence.
A read from a volatile field is always done before all other memory accesses
wich occur after it in the instruction sequence.

A volatile field as a simple way to flag, that memorymanipulations are over.

following an example from the specs:

using System;
using System.Threading;
class Test
{
public static int result;
public static volatile bool finished;
static void Thread2() {
result = 143;
finished = true;
}

static void Main() {
finished = false;
// Run Thread2() in a new thread
new Thread(new ThreadStart(Thread2)).Start();
// Wait for Thread2 to signal that it has a result by setting
// finished to true.
for (;;) {
if (finished) {
Console.WriteLine("result = {0}", result);
return;
}
}
}
}

Since finished is volatile, in method Thread2 the write to result will
allways occur before the write to finished and in method Main the read from
finished will allways occur before the read from result, so the read from
result in Main can''t occur before the write in Thread2.

HTH

Christof


Often, if just one thread is writing to the object (and other
threads just reading it),you can get away with using just volatile.

Generally, the shared object would need to be an atomic value, so the
reader may see it sudden change from state A to state B, but would
never see it half-way between A & B.


这篇关于什么时候是“易变的”用来代替“锁定” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:09