锁定与否? [英] To lock or not?

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

问题描述

我有一个课程如下:


公共课OperationFeedback

{

DateTime _startTime;


public DateTime StartTime

{

get

{

return _startTime;

}

设定

{

_startTime = value;

}

}


}


设置将在后台线程中进行。从我的GUI

线程中获取。我应该在get和set中实现一个锁吗?如果我这样做为什么?


谢谢,


- 弗里茨

I have a class as follows:

public class OperationFeedback
{
DateTime _startTime;

public DateTime StartTime
{
get
{
return _startTime;
}
set
{
_startTime = value;
}
}

}

Set will occur in a background thread. Getting will occur from my GUI
thread. Should I implement a lock inside the get and set? If I so why?

Thanks,

- Fritz

推荐答案

Fritz,


如果StartTime属性是你需要同步的唯一一个项目,那么我会说在课堂上很好。但是,作为一般的

指南,我认为很少有类应该这样做(即使这个

一个)。这样做的原因是操作通常比简单的get / sets更复杂,并且你真的应该将整个操作包装在锁定块中以确保整个操作是同步的,而不仅仅是b $ b的一部分。


当然,如果你把你的操作封装得足够好,你可以

同步访问班级中的人。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


< fr ********@gmail.com在留言中写道

新闻:13 ************************* ********* @ i12g2000 prf.googlegroups.com ...
Fritz,

If the StartTime property is the only one item that you need to
synchronize, then I would say in the class is fine. However, as a general
guideline, I think that there are few classes that should do this (even this
one). The reason for this is that operations are usually more complex than
simple get/sets, and you really should be wrapping those entire operations
in lock blocks to make sure that the whole operation is synchronized, not
just one part of it.

Of course, if you encapsulate your operations well enough, you can
synchronize access to those in a class.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<fr********@gmail.comwrote in message
news:13**********************************@i12g2000 prf.googlegroups.com...

>我的课程如下:


公共类OperationFeedback

{

DateTime _startTime;


public DateTime StartTime

{

get

{

返回_startTime;

}

设置

{

_startTime = value;

}

}


}


设置将在后台线程中进行。从我的GUI

线程中获取。我应该在get和set中实现一个锁吗?如果我这样做为什么?


谢谢,


- Fritz
>I have a class as follows:

public class OperationFeedback
{
DateTime _startTime;

public DateTime StartTime
{
get
{
return _startTime;
}
set
{
_startTime = value;
}
}

}

Set will occur in a background thread. Getting will occur from my GUI
thread. Should I implement a lock inside the get and set? If I so why?

Thanks,

- Fritz



如果我按原样离开课程?是否有可能在获取时从set中间的一个线程调用
从另一个开始,

该值可能包含部分旧值和部分新值

(因为DateTime不是原子的)。如果我想确保一个正确的价值

,当我获取时,我怀疑我需要锁定(这就是我想要的确定/理解的b $ b)。


- Fritz
If I leave the class as is? Is it possible that when the "get" is
called from one thread in the middle of the "set" from another, that
the value could contain part of the old and part of the new value
(since DateTime is not atomic). If I want to ensure a proper value
when I "get", I suspect I need to lock (this is what I am trying to
confirm / understand).

- Fritz


该值可能包含部分新旧值和部分新值

所以你是说你担心存储的值是

" 3/3/2003 3:33 PM"然后调用set方法设置一个新值

,比如说5/5/2005 5:55 AM并且在此过程中调用get方法并且

最终读取类似5/5/2005 3:33 PM的内容。 (包含部分旧的

和部分新值)?


我是否完全误解了你的问题? :)
the value could contain part of the old and part of the new value

So are you saying that you are worried about having a stored value of say
"3/3/2003 3:33 PM" and then having the set method called to set a new value
of say "5/5/2005 5:55 AM" and during this process call the get method and
end up reading something like "5/5/2005 3:33 PM" (contain part of the old
and part of the new value)?

Did I totally misunderstand your question? :)


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

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