I have a class that stores a dictionary. A thread may edit this dictionary in another function. Given that it is writing to this dictionary, I make sure that it is wrapped in a lock. For instance:
public void SetModuleLogLevel(string module, LogLevel logLevel)
{
if (module == "Other" || !this._moduleToLogLevel.ContainsKey(module))
return;
lock (this._lock)
{
this._moduleToLogLevel[module] = logLevel;
}
}
I ahve another function that returns the value from this dictionary. For example,
private bool IsUrgentToLog(string module, LogLevel logLevel)
{
if (!this._moduleToLogLevel.ContainsKey(module)) return false;
lock (this._lock)
{
if (this._moduleToLogLevel[module] < logLevel) return true;
}
return false;
}
Given that I'm only reading from this dictionary in this function, does it need a lock?