锁定声明问题 [英] lock statement questions

查看:71
本文介绍了锁定声明问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在相关问题上获得了很多信息。此链接

< http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>来自Brian

Gideon特别有用。


1)锁定声明*不*让处理器完成
$ b中的所有代码$ b代码块之前给你的进程另一个线程一块处理器时间,它只能确保其他线程无法运行相同的代码

*如果*他们也坚持到同一个锁对象。 (true / false) - 我认为是真的


2)锁定语句无效,除非它用于* all *访问共享资源

; * *读取和写入访问。 (真/假) - 我认为

true


有人可以确认我对此有正确理解吗?

I got great info on related question previously. This link
<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian
Gideon was especially informative.

1) the lock statement does *not* make the processor finish all code in the
code block before giving another thread from your process a slice of
processor time, it only makes sure other threads cannot run the same code
*if* they also adhere to the same lock object. (true/false) - I think true

2) the lock statement is not effective unless it is used for *all* access to
the shared resource; *both* read and write access. (true/false) - I think
true

could someone confirm I''m understanding this correctly?

推荐答案

djc,


1)true


2)true

djc <无*** @ nowhere.com>在消息中写道

新闻:OY ************** @ TK2MSFTNGP04.phx.gbl ...
djc,

1) true

2) true
"djc" <no***@nowhere.com> wrote in message
news:OY**************@TK2MSFTNGP04.phx.gbl...
我收到了很多信息相关问题。此链接
< http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>来自Brian
Gideon特别提供了信息。

1)lock语句*不*让处理器完成
代码块中的所有代码,然后再从进程中提供另一个线程a切片的处理器时间,它只能确保其他线程无法运行相同的代码
*如果*它们也坚持同一个锁对象。 (true / false) - 我认为是真的

2)除非用于共享资源的* all * access
,否则lock语句无效; * *读取和写入访问。 (真/假) - 我认为是真的

有人可以确认我对此有正确理解吗?
I got great info on related question previously. This link
<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian
Gideon was especially informative.

1) the lock statement does *not* make the processor finish all code in the
code block before giving another thread from your process a slice of
processor time, it only makes sure other threads cannot run the same code
*if* they also adhere to the same lock object. (true/false) - I think true

2) the lock statement is not effective unless it is used for *all* access
to the shared resource; *both* read and write access. (true/false) - I
think true

could someone confirm I''m understanding this correctly?



见下面的注释。

" djc" <无*** @ nowhere.com>在消息中写道

新闻:OY ************** @ TK2MSFTNGP04.phx.gbl ...
see notes below.
"djc" <no***@nowhere.com> wrote in message
news:OY**************@TK2MSFTNGP04.phx.gbl...
我收到了很多信息相关问题。此链接
< http://www.yoda.arachsys.com/csharp/threads/volatility.shtml>来自Brian
Gideon特别提供了信息。

1)lock语句*不*让处理器完成
代码块中的所有代码,然后再从进程中提供另一个线程a切片的处理器时间,它只能确保其他线程无法运行相同的代码
*如果*它们也坚持同一个锁对象。 (真/假) - 我认为真的是


如果我正确地理解你这是真的,那么锁只会定义一个

临界区(并确保多个线程永远不会在

内同时关键部分)

2)锁定语句无效,除非它用于* all * access
共享资源; * *读取和写入访问。 (真/假) - 我认为是真的
I got great info on related question previously. This link
<http://www.yoda.arachsys.com/csharp/threads/volatility.shtml> from Brian
Gideon was especially informative.

1) the lock statement does *not* make the processor finish all code in the
code block before giving another thread from your process a slice of
processor time, it only makes sure other threads cannot run the same code
*if* they also adhere to the same lock object. (true/false) - I think true
If I am understanding you correctly this is true, the lock simply defines a
critical section (and insures that multiple threads will never be within
that critical section concurrently)
2) the lock statement is not effective unless it is used for *all* access
to the shared resource; *both* read and write access. (true/false) - I
think true




这通常是正确的。同样重要的是要记住很多东西

已经是原子的......


公共类foo {

private SomeObject bar;

public SomeObject Bar {

get {return bar; }

set {bar = value; }

}

}


这些访问器不需要锁定,因为它们是引用的大小或

更小(框架保证是原子的)。



This is generally true. It is also important to remember that many things
are already atomic as an example ..

public class foo {
private SomeObject bar;
public SomeObject Bar {
get { return bar; }
set { bar = value; }
}
}

These accessors will not need locking as they are the size of a reference or
smaller (which is assured to be atomic by the framework).


Greg Young< dr ************* ******@hotmail.com>写道:
Greg Young <dr*******************@hotmail.com> wrote:
2)锁定语句无效,除非它用于共享资源的* all * access
; * *读取和写入访问。 (真/假) - 我认为是真的
2) the lock statement is not effective unless it is used for *all* access
to the shared resource; *both* read and write access. (true/false) - I
think true



这一般都是正确的。同样重要的是要记住很多事情已经原子化了作为一个例子。

公共类foo {
私人SomeObject栏;
公共SomeObject Bar {
get {返回栏; }
设置{bar = value; }
}

这些访问器不需要锁定,因为它们是引用的大小或更小(确保框架是原子的) 。



This is generally true. It is also important to remember that many things
are already atomic as an example ..

public class foo {
private SomeObject bar;
public SomeObject Bar {
get { return bar; }
set { bar = value; }
}
}

These accessors will not need locking as they are the size of a reference or
smaller (which is assured to be atomic by the framework).




不是这样。原子性和波动性不是一回事。请参阅
http://www.pobox。 com / ~siget / csharp / t ... latility.shtml


例如,如果你使用布尔标志来表示一个线程

应该停止处理,并且该标志是从另一个线程设置的,然后

没有锁定或波动,不能保证

线程将看到更改。这是一个例子:


使用System;

使用System.Threading;


class Test

{

静态bool stop = false;


static void Main()

{

新主题(KeepRunning).Start();

Thread.Sleep(1000);

stop = true;

Console.WriteLine(应该很快停止);

}


static void KeepRunning()

{

int i = 0;

while(!stop)

{

i ++;

}

}

}


使用/ o +开关编译以启用优化,并在我的框中编译

至少,这个过程永远不会消亡。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

我f回复小组,请不要给我发邮件



Not true. Atomicity and volatility are not the same thing. See
http://www.pobox.com/~skeet/csharp/t...latility.shtml

For instance, if you use a boolean flag to indicate that a thread
should stop processing, and that flag is set from another thread, then
without either locking or volatility there is no guarantee that the
thread will ever see the change. Here''s an example:

using System;
using System.Threading;

class Test
{
static bool stop=false;

static void Main()
{
new Thread(KeepRunning).Start();
Thread.Sleep(1000);
stop = true;
Console.WriteLine ("Should stop very soon");
}

static void KeepRunning()
{
int i=0;
while (!stop)
{
i++;
}
}
}

Compile with the /o+ switch to enable optimisations, and on my box at
least, the process never dies.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于锁定声明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆