C# lock Keyword (Execute Code on Only One Thread)

Use the lock keyword to restrict concurrent access and develop correct threaded programs.
Lock. This keyword is used in threading. It restricts code from being executed by more than one thread at the same time. This makes threaded programs reliable.Keywords
The lock statement uses a special syntax form to restrict concurrent access. Lock is compiled into a lower-level implementation based on threading primitives.
An example. The Main method creates 10 new threads, and then calls Start on each one. Please examine method "A" to see the lock statement.

Method A: This uses lock on an object. Each invocation of this method accesses the threading primitives implemented 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.

Info: Method A is invoked 10 times. The output shows the protected method region is executed sequentially—about 100 milliseconds apart.

ThreadStartSleep

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

Static
C# program that uses lock statement 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 100 milliseconds. // ... This is thread serialization. lock (_object) { Thread.Sleep(100); Console.WriteLine(Environment.TickCount); } } static void Main() { // Create ten new threads. for (int i = 0; i < 10; i++) { ThreadStart start = new ThreadStart(A); new Thread(start).Start(); } } } Possible output of the program 28106840 28106949 28107043 28107136 28107246 28107339 28107448 28107542 28107636 28107745
IL. Let's examine the intermediate representation for the lock statement. In compiler theory, high-level source texts are translated to lower-level streams of instructions.IL

Tip: The lock statement here is 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.

TryFinally
Intermediate representation for method using lock: .method private hidebysig static void A() cil managed { .maxstack 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 100 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 lock to synchronize accesses, we create a communication between time and state. The state is connected to the concept of time and sequential accesses to the lock.

Also: In the Theory of Relativity, there is a communication between time and state.

Info: This is the speed of light, which is a constant based on the relation of time and space.

Locks: This connection (between time and space) is present also in locks—in threading constructs.

Relativity, continued. For a better description of how relativity mirrors concurrent synchronization, read the wizard book. This is the Structure and Interpretation of Computer Programs.
A summary. Lock is a synchronization construct. We looked at an example and stepped into the IL. We related the Theory of Relativity and the complexities of the universe to threading.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls