Image of this code:
You have 2 arrays and you need to lock both of them at the same moment (for some reason - you just need to lock both of them because they are somehow dependent on each other) - you can insert a lock
lock (array1) { lock (array2) { ... do your code } }
but this can lead to a deadlock if someone from another part of your code executes
lock (array2) { lock (array1) { ... do your code } }
and array 1 was blocked - the execution context switched - then array 2 was blocked by the second thread.
Is there any way to atomize them? such as
lock_array(array1, array2) { .... }
I know that I can just create an additional โlock objectโ and lock it, and not for all arrays in my code, but this seems to me wrong ...
source share