zoukankan      html  css  js  c++  java
  • Thread safe 深入理解lock

    The lock and SyncLock Keywords

    1 ). Thread to start method with parameter, the method must be with object type parameter.

    2).  The lock (C#) and SyncLock (Visual Basic) statements can be used to ensure that a block of code runs to completion without interruption by other threads. This is accomplished by obtaining a mutual-exclusion lock for a given object for the duration of the code block.

    http://www.cnblogs.com/fangzheng/archive/2010/03/29/1699799.html
    http://www.cnblogs.com/fangzheng/archive/2010/06/07/1750205.html
    http://msdn.microsoft.com/en-us/library/ms173179.aspx#Y370 

     

    A lock or SyncLock statement is given an object as an argument, and is followed by a code block that is to be executed by only one thread at a time. For example:

    public class TestThreading
    {
        private System.Object lockThis = new System.Object();

        public void Process()
        {

            lock (lockThis)
            {
                // Access thread-sensitive resources.
            }
        }

    }

    The argument provided to the lock keyword must be an object based on a reference type, and is used to define the scope of the lock.

        In the example above, the lock scope is limited to this function because no references to the object lockThis exist outside the function. If such a reference did exist, lock scope would extend to that object.

        Strictly speaking, the object provided is used solely to uniquely identify the resource being shared among multiple threads, so it can be an arbitrary class instance. In practice, however, this object usually represents the resource for which thread synchronization is necessary. For example, if a container object is to be used by multiple threads, then the container can be passed to lock, and the synchronized code block following the lock would access the container. As long as other threads locks on the same contain before accessing it, then access to the object is safely synchronized.

    Generally, it is best to avoid locking on a public type, or on object instances beyond the control of your application. For example, lock(this) can be problematic if the instance can be accessed publicly, because code beyond your control may lock on the object as well. This could create deadlock situations where two or more threads wait for the release of the same object. Locking on a public data type, as opposed to an object, can cause problems for the same reason. Locking on literal strings is especially risky because literal strings are interned by the common language runtime (CLR). This means that there is one instance of any given string literal for the entire program, the exact same object represents the literal in all running application domains, on all threads. As a result, a lock placed on a string with the same contents anywhere in the application process locks all instances of that string in the application. As a result, it is best to lock a private or protected member that is not interned. Some classes provide members specifically for locking. The Array type, for example, provides SyncRoot. Many collection types provide a SyncRoot member as well.

    For more information about the lock and SyncLock statements, see the following topics:

    做个快乐的自己。
  • 相关阅读:
    代码4
    readline,readlines,read函数
    代码3
    find函数
    字典的循环和if语句
    代码2
    代码1
    python除法
    字符串
    print函数
  • 原文地址:https://www.cnblogs.com/Jessy/p/2269421.html
Copyright © 2011-2022 走看看