Use of the lock keyword in C #

Source: Internet
Author: User

The Lock statement in C # provides synchronization functionality by implicitly using Monitor. The Lock keyword calls Enter at the beginning of the block, and Exit is called at the end of the block.

In general, you should avoid locking the public type, or the instance will go beyond the control of your code. Common structure Lock (this), Lock (typeof (MyType)) and lock ("MyLock") violate this guideline:

    • If the instance can be accessed publicly, the lock (this) issue occurs.
    • If MyType can be accessed publicly, a lock (typeof (MyType)) issue will occur.
    • The Lock ("MyLock") issue occurs because any other code that uses the same string in a process will share the same lock.

The best practice is to define private objects to lock, or private static object variables to protect data that is common to all instances.

Summarize:

1, if two operations are interactive, such as reading and writing a file, can only allow one execution, the lock object should be the same.

Because lock implies monitor, the Monitor class controls access to an object by granting an object lock to a single thread. Object locks provide the ability to restrict access to code blocks (often called critical sections). When a thread owns a lock on an object, no other thread can acquire the lock. You can also use Monitor to ensure that no other thread is allowed to access the section of the application code that is being executed by the owner of the lock, unless another thread is executing the code with another locked object.

2, if two operations are mutually irrelevant, then the lock object should be different, if the same lock, will directly affect the execution of other operations.

In our development process, often for the sake of convenience, but only to create a lock object, and some in the base class to create a lock, sub-class common, this is in the system architecture using Factory mode, often appear misunderstanding. If it is an unrelated operation, the execution of an operation must wait for another operation to finish before it is executed, which is bound to be affected by the lock, greatly reducing the performance of the system and sometimes causing deadlocks.

3, lock itself also has a system loss.

Lock itself also needs to utilize resources, so unnecessary locks can degrade the performance of the system. In this experiment, the results of locking and locking are different, and the result of the lock output is shortened. You can also write a small example to test it yourself. So use the lock must be cautious, can not be abused.

Use of the lock keyword in C #

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.