java的synchronized关键字的C#版本? [英] C# version of java's synchronized keyword?

查看:33
本文介绍了java的synchronized关键字的C#版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c# 是否有自己版本的 java同步"关键字?

即在 Java 中,它可以指定为函数、对象或代码块,如下所示:

public synchronized void doImportantStuff() {//危险代码放在这里.}

public void doImportantStuff() {//琐碎的东西同步(一些锁){//危险代码放在这里.}}

解决方案

首先 - 大多数类永远不需要是线程安全的.使用 YAGNI:只有在你知道你真的要使用它时才应用线程安全(并进行测试).

对于方法级的东西,有[MethodImpl]:

[MethodImpl(MethodImplOptions.Synchronized)]public void SomeMethod() {/* 代码 */}

这也可以用于访问器(属性和事件):

private int i;public int SomeProperty{[MethodImpl(MethodImplOptions.Synchronized)]得到{返回我;}[MethodImpl(MethodImplOptions.Synchronized)]设置{我=值;}}

请注意,字段类事件默认是同步的,而自动实现的属性不是:

public int SomeProperty {get;set;}//不同步公共事件 EventHandler SomeEvent;//同步

就我个人而言,我不喜欢 MethodImpl 的实现,因为它锁定了 thistypeof(Foo) - 这违反了最佳实践.首选选项是使用您自己的锁:

私有只读对象syncLock = new object();public void SomeMethod() {锁(同步锁){/* 代码 */}}

注意对于field-like事件,锁定的实现依赖于编译器;在较旧的 Microsoft 编译器中,它是 lock(this)/lock(Type) - 但是,在最近的编译器中它使用 Interlocked 更新 - 所以线程安全,没有讨厌的部分.

这允许更精细的使用,并允许使用 Monitor.Wait/Monitor.Pulse 等在线程之间进行通信.

相关的博客条目(稍后重新访问).>

Does c# have its own version of the java "synchronized" keyword?

I.e. in java it can be specified either to a function, an object or a block of code, like so:

public synchronized void doImportantStuff() {
   // dangerous code goes here.
}

or

public void doImportantStuff() {
   // trivial stuff

   synchronized(someLock) {
      // dangerous code goes here.
   }
}

解决方案

First - most classes will never need to be thread-safe. Use YAGNI: only apply thread-safety when you know you actually are going to use it (and test it).

For the method-level stuff, there is [MethodImpl]:

[MethodImpl(MethodImplOptions.Synchronized)]
public void SomeMethod() {/* code */}

This can also be used on accessors (properties and events):

private int i;
public int SomeProperty
{
    [MethodImpl(MethodImplOptions.Synchronized)]
    get { return i; }
    [MethodImpl(MethodImplOptions.Synchronized)]
    set { i = value; }
}

Note that field-like events are synchronized by default, while auto-implemented properties are not:

public int SomeProperty {get;set;} // not synchronized
public event EventHandler SomeEvent; // synchronized

Personally, I don't like the implementation of MethodImpl as it locks this or typeof(Foo) - which is against best practice. The preferred option is to use your own locks:

private readonly object syncLock = new object();
public void SomeMethod() {
    lock(syncLock) { /* code */ }
}

Note that for field-like events, the locking implementation is dependent on the compiler; in older Microsoft compilers it is a lock(this) / lock(Type) - however, in more recent compilers it uses Interlocked updates - so thread-safe without the nasty parts.

This allows more granular usage, and allows use of Monitor.Wait/Monitor.Pulse etc to communicate between threads.

A related blog entry (later revisited).

这篇关于java的synchronized关键字的C#版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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