Lock ()

Source: Internet
Author: User

It's all written on the Internet.
Private Object Thislock = new Object ();
Lock (Thislock)
{
...
}
So what's the syntax in lock () brackets, what curly braces {} should be written
If I have a funlog () method and then there are multiple threads that use this method, how do I lock this method?

Do not put big theory, the theory of Cardinal himself went to MSDN study.
Lock is to lock the code snippet in {} To prevent multiple threads from running the code snippet at the same time.
Usage:

 private  void   do () {threat T1  =new   threat (dosomething); threat T2  =new   Threat (dosomething); t1. Start (); T2. Start ();}  object  locker=object   ();  private  void   DoSomething () {... .....").  lock   (Locker) {Funlog ();} ........}

For example, when T1 executes to lock, the detection discovers that locker is not in the locked list, it records Locker's memory address, joins to the lock list, and begins to execute lock code snippet; T2 again to lock, Detection locker memory address is in the locked list, so the thread waits, T1 finish Funlog reach lock end, the locker address from the lock list, t2 detection locer is not in the lock list, it records locker memory address, Add to the lock list and enter the code snippet that performs the lock.

Lock (...) must be an object inside the parentheses????

Lock (...) The content in parentheses should be a reference type, and C # 's value types include: struct (numeric type, bool type, user-defined struct), enumeration, nullable type. The reference types of C # include: arrays, user-defined classes, interfaces, delegates, object, strings.
So


    {    }structs{}inti;Objecto;stringstr; A A=NewA (); s S=NewS ();Lock(i) {}//ErrorLock(o) {}//correctLock(str) {}//correctLock(a) {}//correctLock(s) {}//Error

Lock is primarily used to handle concurrency problems and lock exclusive objects.
Lock the current instance: Lock (This)
Lock all instances of this class: Lock (typeof ([Type]))
A lock on a string that locks all strings of the same content, suggesting that static strings should be used instead of
The lock keyword is simpler than monitor, in fact lock is a package for the Enter search and exit of monitor
Lock is a simple and easy way to synchronize threads

 Public void Function () {objectnewobject(); Lock (LockThis) {}} can also be used Monitor,mutex,readerwriterlock

Lock ()

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.