Making two dictionary writes atomic in multithreaded context
I have two writes to two
ConcurrentDictionary
ConcurrentDictionary
s, one after the other:
dict1[k1] = o;dict2[k2] = o;
dict1[k1] = o;dict2[k2] = o;
Importantly, they write the same value in two different dictionaries with two different keys, which is important. Problem is that methods read from one dictionary or the other (they don't really utilize both at once) and the sequence of these writes is not atomic, meaning I could get a state where
dict1
dict1
contains
o
o
but
dict2
dict2
does not, which is undesirable. Is there any more efficient way to deal with this outside of needing to define a lock for the two of them?