Asp.net C # Lock

Source: Internet
Author: User

The lock keyword can be used to ensureCodeThe block is running without being interrupted by other threads. This is achieved by obtaining mutex locks for a given object during the code block operation.
There is an object as a parameter, and there is a code block that can only be executed by one thread at a time after this parameter
The parameter must be a reference-type object. This object is used to define the lock range.
In the above example [private void function () {lock (OBJ) {}}], the lock range is limited to this function because no reference to this object exists outside the function.
The lock object is only used to uniquely identify resources shared by multiple threads, so it can be any class instance.
This object usually indicates the resources that require thread synchronization.
Eg: If a container object will be used by multiple threads, the container can be passed to lock, and the synchronization code block after lock will access the container. As long as other threads lock the container before accessing the container, access to the object will be synchronized securely.

It is best to avoid locking the public type or locking the application.ProgramIf the instance can be publicly accessed, lock (this) may be faulty because uncontrolled Code may also lock the object. This may cause
Deadlock [two or more threads waiting to release the same object]
Locking public data types (compared with objects) may also cause problems
Locking a string is especially dangerous because the string is temporarily stored by the Common Language Runtime Library (CLR), which means that there is only one instance for any given string in the entire program.
The same object indicates the text in all threads of all running application domains.
If a lock is placed on a string with the same content anywhere in the application process, all instances of the string in the application will be locked.
Some classes provide members dedicated for locking. For example, the array type provides syncroot. Many Collection types also provide syncroot
Private Static object synccslistlock = new object ();.
Lock (synctslock)

lock (x)
{< br> dosomething ();
}< br> equivalent to
system. object OBJ = (system. object) x;
system. threading. monitor. enter (OBJ);
try
{< br> dosomething ();
}< br> finally
{< br> system. threading. monitor. exit (OBJ);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.