En el siguiente código:ContainsKey hilo de seguridad
public class StringCache
{
private readonly object lockobj = new object();
private readonly Dictionary<int, string> cache = new Dictionary<int, string>();
public string GetMemberInfo(int key)
{
if (cache.ContainsKey(key))
return cache[key];
lock (lockobj)
{
if (!cache.ContainsKey(key))
cache[key] = GetString(key);
}
return cache[key];
}
private static string GetString(int key)
{
return "Not Important";
}
}
1) ¿Es seguro para hilos ContainsKey? IOW, ¿qué sucede si ese método se está ejecutando cuando otro hilo está agregando algo al diccionario? 2) Para la primera memoria caché de devolución [clave], ¿hay alguna posibilidad de que pueda devolver un valor ilegible?
TIA,
MB
Enlace no encontrado (404). – avenmore