c# - how to prevent a deadlock when you need to lock multiple objects -
image code:
you have 2 arrays, , need lock both of them in same moment (for reason - need keep locked both of them because somehow depending on each other) - nest lock
lock (array1) { lock (array2) { ... code } }
but may result in deadlock in case in other part of code do
lock (array2) { lock (array1) { ... code } }
and array 1 locked - execution context switched - array 2 locked second thread.
is there way atomically lock them? such as
lock_array(array1, array2) { .... }
i know create "lock object" , lock instead of both arrays everywhere in code, doesn't seem correct me...
in general should avoid locking on publicly accessible members (the arrays in case). you'd rather have private static object
you'd lock on.
Comments
Post a Comment