"is this using of dictionary thread-safe or not?" Code Answer


there are 2 problems here:

  • lock (somedictionary) - this is dis-advised, even if the dictionary is not used elsewhere. it is a theoretical argument but the (future) code of the dictionary class could lock on itself.

  • if (!somedictionary (e, out str)) without a lock. i assume this is a call to trygetvalue(). this simply is not thread-safe, your read could be interrupted by a write in another thread. this could end in all sorts of errors (index out of range, null reference). the errors will be very rare (= hard to reproduce).

By Jason Leung on May 23 2022

Answers related to “is this using of dictionary thread-safe or not?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.