当使用在C#中锁的线程? [英] When to use the lock thread in C#?

查看:206
本文介绍了当使用在C#中锁的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理多个传入套接字连接,并且创建存储在XML格式的数据,2个不同的线程的服务器。



我用的是锁定语句线程安全。可悲的是使用这种方法我的应用程序显著减慢。



我试图在所有不使用锁定和服务器非常快速的执行,甚至连文件存储似乎提振;但程序崩溃的原因,我30秒后,不明白 - 1分钟。的工作。



所以。我认为最好的办法是少用锁或哪里还有必要使用它只有在那里。因此,我有2个问题:




  1. 是锁在需要的时候我写公开访问的变量(C#列表)只甚至当我从他们读?


  2. 仅在由套接字句柄创建asyncronous线程或在其他地方所需要的锁呢?




有人能够给我一些实用的指导方针,如何操作。我不会张贴整个代码,这段时间。它没有感知张贴约2500行代码。


解决方案

时锁需要的时候我写信给公众访问的变量(C#列表)只有甚至当我从他们读?




是(甚至当你读)




仅在由套接字句柄创建asyncronous线程或在其他地方呢?

$ b $所需的锁b

是的。无论代码访问该共享一段代码,始终锁定。






这听起来像你可能不被锁定的个人的对象,但锁定的一件事对所有锁的情况。



如果这样就把智能的离散通过创建的关联和仅锁定的同时,不与其他部分的其他线程干扰某些章节。



下面是一个例子:

  //这个类模拟使用两种不同的线程安全的资源,以及如何把它们锁
//线程安全,但不能阻止其他线程获取不同的资源。
公共类SmartLocking
{
私人字符串StrResource1 {搞定;组; }
私人字符串StrResource2 {搞定;组; }

私有对象_Lock1 =新的对象();
私有对象_Lock2 =新的对象();

公共无效DoWorkOn1(字符串变化)
{
锁(_Lock1)
{
_Resource1 =改变;
}
}

公共无效DoWorkOn2(字符串change2)
{
锁(_Lock2)
{
_Resource2 = change2 ;
}
}
}


I have a server which handles multiple incoming socket connections and creates 2 different threads which store the data in XML format.

I was using the lock statement for thread safety almost in every event handler called asyncronously and in the 2 threads in different parts of code. Sadly using this approach my application significantly slows down.

I tried to not use lock at all and the server is very fast in execution, even the file storage seems to boost; but the program crashes for reasons I don't understand after 30sec - 1min. of work.

So. I thought that the best way is to use less locks or to use it only there where's strictly necessary. As such, I have 2 questions:

  1. Is the lock needed when I write to the public accessed variables (C# lists) only or even when I read from them ?

  2. Is the lock needed only in the asyncronous threads created by the socket handler or in other places too ?

Someone could give me some practical guidelines, about how to operate. I'll not post the whole code this time. It hasn't sense to post about 2500 lines of code.

解决方案

Is the lock needed when I write to the public accessed variables (C# lists) only or even when I read from them ?

Yes (even when you read).

Is the lock needed only in the asyncronous threads created by the socket handler or in other places too ?

Yes. Wherever code accesses a section of code which is shared, always lock.


This sounds like you may not be locking individual objects, but locking one thing for all lock situations.

If so put in smart discrete locks by creating individual unique objects which relate and lock only certain sections at a time, which don't interfere with other threads in other sections.

Here is an example:

// This class simulates the use of two different thread safe resources and how to lock them
// for thread safety but not block other threads getting different resources.
public class SmartLocking
{
    private string StrResource1 { get; set; }
    private string StrResource2 { get; set; }

    private object _Lock1 = new object();
    private object _Lock2 = new object();

    public void DoWorkOn1( string change )
    {
        lock (_Lock1)
        {
            _Resource1 = change;
        }
    }

    public void DoWorkOn2( string change2 )
    {
        lock (_Lock2)
        {
            _Resource2 = change2;
        }
    }
}

这篇关于当使用在C#中锁的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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