(C #) Lock-Locks The object, mutex multiple threads, and makes synchronization.

Source: Internet
Author: User
Tags mscorlib

C # Lock

Original: Http://www.dotnetperls.com/lock

Locking is essential in threaded programs. It restricts code from being executed to more than one thread at the same time. This makes threaded programs reliable. The lockStatement uses a special syntax form to restrict concurrent access.

Keywords

Note: Lock is compiled to a Lower-level implementation based on threading primitives.

Example

Here we see a static method "A", that uses, the lock statement on an object. When the method A was called many times on new threads, each invocation of the method accesses the threading primitives Imp Lemented by the lock.

Then : Only one method A can call the statements protected by the lock at A single time, regardless of the thread count.

Program that uses lock statement:c#using system;using system.threading;class program{    static readonly Object _object = new Object ();    static void A ()    {//Lock on the readonly object.//... Inside the lock, sleep for milliseconds.//... This is the thread serialization. Lock (_object) {    thread.sleep (+);    Console.WriteLine (Environment.tickcount);}    }    static void Main ()    {//Create ten new threads. for (int i = 0; i < i++) {    ThreadStart start = new ThreadStart (A);    New Thread (start). Start ();}}    } Possible output of the program 28106840281069492810704328107136281072462810733928107448281075422810763628107745

in this example, The Main method creates ten new threads, and then calls Start in each one. The method A is invoked ten times and the tick count shows the protected method is executed Sequentially-about 100 Milliseconds apart.

Note: If you remove the lock statement, the methods is executed all at once and with no synchronization.

Intermediate representation

Let's examine the intermediate representation for the lock statement in the above example method A. In compiler theory, high-level source texts is translated to lower-level streams of instructions.

Tip: The lock statement here's transformed into calls to the static methods Monitor.Enter and Monitor.Exit.

Also: The lock is actually implemented with a try-finally construct. This uses the exception handling control flow.

Intermediate representation for method using lock. Method private hidebysig static void A () CIL managed{    . maxs Tack 2    . Locals init ([0] object Obj2)    l_0000:ldsfld object Program::_object    l_0005:dup    l_0006:stloc.0    l_0007:call void [Mscorlib]system.threading.monitor::enter (object)    L_000C:LDC.I4.S    l_000e:call void [Mscorlib]system.threading.thread::sleep (Int32)    l_0013:call int32 [ Mscorlib]system.environment::get_tickcount ()    l_0018:call void [Mscorlib]system.console::writeline (Int32)    L_001D:LEAVE.S l_0026    l_001f:ldloc.0    l_0020:call void [Mscorlib]system.threading.monitor::exit (object)    l_0025:endfinally    L_0026:ret    . Try l_000c to l_001f finally handler l_001f to l_0026}
Relativity

By using the lock statement to synchronize accesses, we is creating a communication between time and state. The state was connected to the concept of time and sequential accesses to the lock.

in the theory of relativity, There is also a communication between time and state. The speed of light, which are a constant based on the relation of time and space. This connection are present also in locks-in threading constructs.

Tip: For a better description of what relativity mirrors concurrent synchronization, please see the wizard book.

Summary

We examined the lock statement in the C # language, first seeing its usage in a example program, and then describing this Synchronization. Next, we stepped into the intermediate representation and their meaning in compiler theory.

Finally: We related the theory of relativity and the complexities of the physical universe to the lock statement.

(C #) Lock-Locks The object, mutex multiple threads, and makes synchronization.

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.