C # streams and sync

I have a private static field that I use for synchronization (blocking). Now I have two functions that I do not want to perform at the same time. So I did this:

public class Synchronization { private static object _lock = new object(); public void MethodA() { lock (_lock) { Console.WriteLine("I shouldn't execute with MethodB"); } } public void MethodB() { lock (_lock) { Console.WriteLine("I shouldn't execute with MethodA"); } } } 

I know that locking on an object will prevent the parallel execution of one function, but will it be the same if I use the same locking object in different methods that are executed at the same time? Simply, can any other thread get a lock on an already locked object in another function?

+4
source share
5 answers

Only one thread at a time can get a lock, so this state is exclusive to all threads on one lock instance. Thus, in your example, at any given time, for all instances of the Synchronization class, only one method object can be executed, since your lock is static. If you want to lock an instance of your class, do not put the lock object as static.

Your assumptions regarding timing are correct.

Note that you must mark the readonly lock object for a completely waterproof solution. As the code stands, you could reassign the lock object and thus break the lock semantics, for example:

 public class Synchronization { private static object _lock = new object(); public void MethodA() { lock (_lock) { Console.WriteLine("I shouldn't execute with MethodB"); } } public void MethodB() { //This shouldn't be allowed! _lock = new object(); lock (_lock) { Console.WriteLine("I shouldn't execute with MethodA"); } } } 

The lock object should be marked readonly , readonly .:

 private static readonly object _lock = new object(); 
+5
source

I believe that you are reading the Blocking Statement on MSDN correctly.

+2
source

You are doing it right. You have created two critical sections that will not be entered at the same time.

Thus, MethodA and MethodB will not be simultaneously active. And only one MethodA (and MethodB) will be active.

This is valid for all created objects. I mean: only one thread will be in any MethodA or MethodB from any object. If you want to lock only inside one object, you can make the _lock object non-static.

+1
source

Locks are provided based on the object that is the lock object, not the method in which the lock statement is executed. That way, in your case, multiple threads can enter different methods, but only one thread at a time can execute any code in the lock statement.

+1
source

Firstly, _lock should not be static. Or do you want multiple instances of the object to block each other? Secondly, you should have only one synchronized method in the class. Moreover, you should avoid dependencies between synchronized methods in your class. Otherwise, you risk that your method caller will do this incorrectly and get unexpected behavior.

Consider, for example, this code:

 class Synchronized { object lockObj = new object(); int counter = 100; public void Decrement() { lock (this.lockObj) { this.counter--; } } public int IsZero() { lock (this.lockObj) { return this.counter == 0; } } } 

Now, what will do with the shared synchronized instance?

Use it like this:

 while (!synchronized.IsZero()) { synchronized.Decrement(); } 

Now thread 1 calls Decrement, the counter gets 0 and immediately thread 2 calls Decrement, because it was expecting a lock in the Decrement method, not IsZero. The counter is now -1, and the loop is infinite.

Not that the locking mechanism was incorrectly programmed, but the caller did not use it well. If you only exposed one synchronized method in your synchronous class, you would not deceive the programmer to blindly believe it.

It should be something like this:

 class Synchronized { object lockObj = new object(); int counter = 100; public bool IfNotZeroDecrement() { lock (this.lockObj) { if (this.counter > 0) this.counter--; return this.counter > 0; } } } /// Usage: while (synchronized.IfZeroDecrement()) { } 
+1
source

Source: https://habr.com/ru/post/1337111/


All Articles