how to prevent a deadlock when you need to lock multiple objects
<p>Image this code:</p>

<p>You have 2 arrays, and you need to lock both of them in same moment (for any reason - you just need to keep locked both of them because they are somehow depending on each other) - you could nest the lock</p>

<pre><code>lock (array1)
{
    lock (array2)
    {
        ... do your code
    }
}
</code></pre>

<p>but this may result in a deadlock in case that someone in other part of your code would do</p>

<pre><code>lock (array2)
{
    lock (array1)
    {
        ... do your code
    }
}
</code></pre>

<p>and array 1 was locked - execution context switched - then array 2 was locked by second thread.</p>

<p>Is there a way to atomically lock them? such as</p>

<pre><code>lock_array(array1, array2)
{
    ....
}
</code></pre>

<p>I know I could just create some extra "lock object" and lock that instead of both arrays everywhere in my code, but that just doesn't seem correct to me...</p>
