什么恰恰做了"锁定"锁? [英] What precisely does a "lock" lock?

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

问题描述

我的工作是创建并管理多个插座连接,这将是从插座收到的主线程,反之亦然转发信息的类。在这里我顺便如此多的信息,并进行多线程是新的给我(这是在C#中的大多数事情),所以我需要的究竟是什么锁定做了澄清。

I'm working on a class that creates and manages multiple socket connections and which will be forwarding information received from the sockets to the main thread and vice-versa. Multi-threading where I'm passing so much information in and out is new to me (as is most things in C#), so I need a clarification on what exactly "locks" do.

当我锁定的方法的部分它只能保证没有其他线程可以输入的代码段或它实际上阻止该代码段的所有变量被其他线程正在修改无论身在何处?发生

When I lock a section of a method does it only ensure that no other thread can enter that section of code or does it actually prevent all the variables in that section of code from being modified by other threads no matter where they occur?

例如:

public class TestLock
{
    public volatile int a = 0;
    public volatile int b = 2;
    private object ALock = new Object();

    public TestLock() { }

    public void UnlockEdit(int atemp, int btemp)
    {
        a = atemp;
        b = btemp;
    }

    public void LockedEdit(int atemp, int btemp)
    {


        lock(ALock)
        {
            a = atemp;
            b = btemp;
        }
    }

    public int[] ReturnValues()
    {
        int[] ret = new int[2];

        lock (ALock)
        {
            ret[0] = a;
            ret[1] = b;
        }

        return ret;
    }

}

如果线程A调用LockedEdit 方法和到达锁定稍稍前线程B进入UnlockEdit。发生了什么?该锁可防止线程B从修改和b?它是否会阻止?或不锁定只适用于代码一个特定分支?或者我应该适用相同的锁定对象(ALock),以每一个读取和写入我想修改,并从多个线程读取?

If thread A calls the "LockedEdit" method and reaches the lock just slightly before thread B comes into UnlockEdit. What happens? Will the lock prevent thread B from modifying a and b? And will it block? Or does the lock only apply to the one particular branch of code? Or should I apply the same "lock" object (ALock) to every single read and write method for the objects I want to modify and read from multiple threads?

推荐答案

借助锁定语句创建一个<一个HREF =http://en.wikipedia.org/wiki/Critical_section相对=nofollow>在代码中的关键部分。这可以防止其它线程进入锁语句括号内的代码。其他线程基本上等到轮到他们进入部分。

The lock statement creates a critical section in your code. This prevents other threads from entering the code inside the lock statements brackets. Other threads basically wait until it is their turn to enter the section.

这不会阻止其他线程中的类修改变量。例如,TestLock.a和TestLock.b可以通过不使用的锁(ALock)语句TestLock类中的功能进行修改。另外,由于变量a和b都是公开它们可以通过代码的类(在单独的线程)的外部修改。

It does not prevent other threads from modifying variables in the class. For instance, TestLock.a and TestLock.b could be modified by a function inside the TestLock class that does not use the Lock(ALock) statement. ALso, since variables a and b are public they could be modified by code outside the class (on a separate thread).

下面是一个很好的链接到.NET <一HREF =http://msdn.microsoft.com/en-us/library/ms173178.aspx相对=nofollow>基本线程:

Here is a good link to .NET Basic Threading:

从MSDN:

lock关键字确保在另一个线程处于临界区一个线程不会进入代码的临界段。如果另一个线程试图进入锁定的代码,它会等待,块,直到该对象被释放。

下面是一些代码,保持一个例子变量安全的多线程应用程序(从 C#3.0菜谱<拍摄/ A>):

Here is an example of some code that keeps variables safe for multithreaded applications (Taken from the C# 3.0 cookbook):

    public static class SaferMemberAccess
        {

            private static int numericField = 1;
                 private static object syncObj = new object( );

            public static void IncrementNumericField( )
           {
                     lock(syncObj)              
                         {
                       ++numericField;
                     }
           }

            public static void ModifyNumericField(int newValue)
            {
                     lock(syncObj)             
                         {
                       numericField = newValue;
                     }
            }

            public static int ReadNumericField( )
            {
                     lock (syncObj)            
                         {
                       return (numericField);
                     }
            }
        }

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

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