From 8e441919a92dff6b29be6f52bc1fd837f2d7ab2b Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 18 Nov 2022 13:17:36 +0100 Subject: [PATCH] add 1.2.4 lock construct in CSharp project --- ADIS_Csharp/ConsoleApp/Program.cs | 36 ++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ADIS_Csharp/ConsoleApp/Program.cs b/ADIS_Csharp/ConsoleApp/Program.cs index 942b3a2..0f3c322 100644 --- a/ADIS_Csharp/ConsoleApp/Program.cs +++ b/ADIS_Csharp/ConsoleApp/Program.cs @@ -132,7 +132,7 @@ class ThreadStop2 #endif -#if true +#if false class SimpleBlocking { @@ -167,4 +167,38 @@ class SimpleBlocking } +#endif + +#if true + +class LockingConstruct1 { + private static object locker = new object(); + + static void Main() + { + long sum = 0; + + Thread t1 = new Thread(delegate() + { + lock (locker) + { + for (int i = 0; i <= 1000; i++) { + sum += i; + } + } + }); + + Thread t2 = new Thread(delegate() + { + lock (locker) + { + Console.WriteLine("Summe = " + sum); + } + }); + + t1.Start(); + t2.Start(); + } +} + #endif \ No newline at end of file