Lock lock in the end who talk about (I)?

EDITORIAL

Last month has been to get file transfer component, which uses multi-threading technology, but some places do need to operate only one thread, how can we ensure that only one thread of it? The first thought is the concept of lock, most recently in our liking of most of the project team who is also a lock, how to lock? See colleagues using lock (this), there are lock (private static object), then a little confused, lock lock in the end who is the most appropriate?

lock

First, the first official statement Msdn

lock keyword to ensure that when a thread is located in a critical area code when another thread does not enter the critical region. If another thread attempts to enter the lock code, it will have to wait (ie stop) until the object is released.
lock calls Enter key at the beginning of the block, while calling Exit at the end of the block. ThreadInterruptedException triggered, if Interrupt interrupt thread waiting for input lock statement.
Typically, public avoid locking type, instance or beyond the control of the code.

Common structure lock (this), lock (typeof (MyType)) and lock ( "myLock") violate this guideline:
If the instance is publicly accessible, the lock appears (this) issue.
If MyType is publicly accessible, the lock (typeof (MyType)) problems.
Since the process used in any other code the same string will share the same lock, so there are lock ( "myLock") problem.
Best practice is to define private object to lock, or private static object variable to protect data common to all instances.
In the body of the lock statement can not wait to use keywords.

Enter refers Monitor.Enter (acquire an exclusive lock on the specified object.), Exit refers Monitor.Exit (release an exclusive lock on the specified object.)

There are above msdn explanation and Exit methods, it can be speculated, "until the object is released," "the object" shall mean the object lock, the object releases or object changes, the other thread can enter the critical section of code (a I not can be understood?).

In multiple threads, each thread has its own resources, but the code area is shared, that is, each thread can perform the same function. This problem is likely caused by several threads simultaneously execute a function, leading to confusion data, resulting in unpredictable results, so we have to avoid this from happening.

Figuratively, there is such a scenario, squatting toilet building where many companies are small single type, that is, can only go in one person, then in order to avoid a time into a person, then how to do it? Not that a man went after shoving the door on it? So you're in there doing things, people outside can only wait for your liberation is over, to enter. The squatting resources (squatting, toilet paper, etc.) are shared.

The most commonly used is the code lock segments in the following format:

private static object objlock = new object();
lock (objlock )
{
    // code logic to be executed 
}

Why lock object is private it? Or in the toilet as an example, private like, the lock only you have access to, and preferably the lock will not change because of external force, others can not access, so as to ensure that you go in, others went into do not, and if it is public, like you squatting among small single lock is not installed on the inside but on the outside of the installation, others want to enter you can not control, and this is not safe.

lock

Original article cited demo can not explain the problem, the article has deleted the original demo, demo modified to migrate to this article.

 [C # foundation] who talk about lock lock in the end? (Supplement and modify)

The above said is the largest lock object, then it can not lock a value type?

The answer is no, as

Of course, lock (null) is not enough, FIG.

Although the compiler can, but the operation will go wrong.

lock(string)

string is the type of application, from the grammar is not wrong.

But the string lock is especially dangerous because the string is "persistence" common language runtime (CLR). This means that the entire program in any given string is only one example, this is the same object represents all the threads all running application domains in the text. Thus, at any position as long as the application process is placed on the string lock with the same content, it will lock in all instances of the application of the string. Typically, it is best to avoid the type of public or locking the lock object instance from an application control. For example, if the instance can be accessed publicly, the lock (this) can be problematic, because uncontrolled code may also locks the object. This could lead to a deadlock, that is, two or more threads wait for the release of the same object. For the same reason, the lock common data types (compared to the target) can also cause problems. And lock (this) only for the current object is valid, if not reached between multiple objects synchronized effect. lock (typeof (Class)) and lock the string as too broad.

to sum up

About lock on here, a place to note the following points

1, lock the object reference type, except for the string type.

2, lock the recommended practice is to use a static, read-only, private object.

3, to ensure the lock of the object can not be modified externally makes sense, if the lock objects in the external changes, other threads will be smooth, it lost the meaning of the lock.

 

EDITORIAL

Last month has been to get file transfer component, which uses multi-threading technology, but some places do need to operate only one thread, how can we ensure that only one thread of it? The first thought is the concept of lock, most recently in our liking of most of the project team who is also a lock, how to lock? See colleagues using lock (this), there are lock (private static object), then a little confused, lock lock in the end who is the most appropriate?

lock

First, the first official statement Msdn

lock keyword to ensure that when a thread is located in a critical area code when another thread does not enter the critical region. If another thread attempts to enter the lock code, it will have to wait (ie stop) until the object is released.
lock calls Enter key at the beginning of the block, while calling Exit at the end of the block. ThreadInterruptedException triggered, if Interrupt interrupt thread waiting for input lock statement.
Typically, public avoid locking type, instance or beyond the control of the code.

Common structure lock (this), lock (typeof (MyType)) and lock ( "myLock") violate this guideline:
If the instance is publicly accessible, the lock appears (this) issue.
If MyType is publicly accessible, the lock (typeof (MyType)) problems.
Since the process used in any other code the same string will share the same lock, so there are lock ( "myLock") problem.
Best practice is to define private object to lock, or private static object variable to protect data common to all instances.
In the body of the lock statement can not wait to use keywords.

Enter refers Monitor.Enter (acquire an exclusive lock on the specified object.), Exit refers Monitor.Exit (release an exclusive lock on the specified object.)

There are above msdn explanation and Exit methods, it can be speculated, "until the object is released," "the object" shall mean the object lock, the object releases or object changes, the other thread can enter the critical section of code (a I not can be understood?).

In multiple threads, each thread has its own resources, but the code area is shared, that is, each thread can perform the same function. This problem is likely caused by several threads simultaneously execute a function, leading to confusion data, resulting in unpredictable results, so we have to avoid this from happening.

Figuratively, there is such a scenario, squatting toilet building where many companies are small single type, that is, can only go in one person, then in order to avoid a time into a person, then how to do it? Not that a man went after shoving the door on it? So you're in there doing things, people outside can only wait for your liberation is over, to enter. The squatting resources (squatting, toilet paper, etc.) are shared.

The most commonly used is the code lock segments in the following format:

private static object objlock = new object();
lock (objlock )
{
    // code logic to be executed 
}

Why lock object is private it? Or in the toilet as an example, private like, the lock only you have access to, and preferably the lock will not change because of external force, others can not access, so as to ensure that you go in, others went into do not, and if it is public, like you squatting among small single lock is not installed on the inside but on the outside of the installation, others want to enter you can not control, and this is not safe.

lock

Original article cited demo can not explain the problem, the article has deleted the original demo, demo modified to migrate to this article.

 [C # foundation] who talk about lock lock in the end? (Supplement and modify)

The above said is the largest lock object, then it can not lock a value type?

The answer is no, as

Of course, lock (null) is not enough, FIG.

Although the compiler can, but the operation will go wrong.

lock(string)

string is the type of application, from the grammar is not wrong.

但是锁定字符串尤其危险,因为字符串被公共语言运行库 (CLR)“暂留”。 这意味着整个程序中任何给定字符串都只有一个实例,就是这同一个对象表示了所有运行的应用程序域的所有线程中的该文本。因此,只要在应用程序进程中的任何位置处具有相同内容的字符串上放置了锁,就将锁定应用程序中该字符串的所有实例。通常,最好避免锁定 public 类型或锁定不受应用程序控制的对象实例。例如,如果该实例可以被公开访问,则 lock(this) 可能会有问题,因为不受控制的代码也可能会锁定该对象。这可能导致死锁,即两个或更多个线程等待释放同一对象。出于同样的原因,锁定公共数据类型(相比于对象)也可能导致问题。而且lock(this)只对当前对象有效,如果多个对象之间就达不到同步的效果。lock(typeof(Class))与锁定字符串一样,范围太广了。

总结

关于lock的介绍就到这里,有下面几点需要注意的地方

1、lock的是引用类型的对象,string类型除外。

2、lock推荐的做法是使用静态的、只读的、私有的对象。

3、保证lock的对象在外部无法修改才有意义,如果lock的对象在外部改变了,对其他线程就会畅通无阻,失去了lock的意义。

 

Guess you like

Origin www.cnblogs.com/linybo/p/11799561.html