Are the following assumptions valid for this code? I put some background info under the code, but I don\'t think it\'s relevant.
Assumption 1: Since th
lock
only has a scope of a single process. If you want to span processes you'll have to use primitives like Mutex
(named).
lock
is the same as Monitor.Enter
and Monitor.Exit
. If you also do Monitor.Enter
and Monitor.Exit
, it's being redundant.
You don't need to lock on read, but you do have to lock the "transaction" of checking if the value doesn't exist and adding it. If you don't lock on that series of instructions, something else could come in between when you check for the key and when you add it and add it--thus resulting in an exception. The lock you're doing is sufficient to do that (you don't need the additional calls to Enter and Exit--lock will do that for you).