c# - Multi-threaded application crash - possible memory corruption? -
i have multi-threaded application in c# uses lock() access dictionary. there 2 threads, consumer , producer. locking mecanism simple. application runs under heavy load weeks without problem.
today crashed. digged windbg see exception , keynotfound when accessing dictionary.
what problems cause crash? should consider memory corruption may occur or not?
making locking fined-grained cause this. example:
bool haskey(string key) { lock (_locker) { return _dictionary.containskey(key); } } int getvalue(string key) { lock (_locker) { return _dictionary[key]; } }
and using this:
void kaboom() { if (haskey("foo")) { int value = getvalue("foo"); // etc.. } }
that won't work, dictionary change between haskey , getvalue call. entire operation needs locked. , yes, goes wrong once month or so.
bool trygetvalue(string key, out int value) { lock (_locker) { if (!haskey(key)) return false; value = getvalue(key); return true; } }
Comments
Post a Comment