The idea is to get a value (“data”) from
- a variable
- if doesn’t exist, then from a file
- if doesn’t exist, then from a server
public static string LoadData(int id)
{
if (_isDataLoaded[id - 1])
{
return _data[id - 1];
}
if (File.Exists(_fileName))
{
bool dataExists;
string cachedData;
if (GetCachedConstant(id, out cachedData)) //read from a file
{
lock (_locker)
{
_isDataLoaded[id - 1] = true;
_data[id - 1] = cachedData;
}
return _data[id - 1];
}
}
string remoteData = GetFromServer(id);
lock (_locker)
{
CacheToFile(id, remoteData); //write to a file
_data[id - 1] = remoteData;
_isDataLoaded[id - 1] = true;
}
return _data[id - 1];
}
This code is used by many threads. Although it seems to be thread safe, actually it isn’t. I tested it, thus, that gives me that idea and makes me sure. There should be double check of existing _data before writing. For example, it should be like double check used in pattern Singleton usually.
Someone please let me understand how do I implement it here?
EDIT:
string[] _data;
bool[] _isDataLoaded.
EDIT2:
The code above might be used with .NET < 4.0, so it’s not allowed to use Lasy there. The only question I have for now, is should I use volatile in case I would use double-checked locking?
volatile string[] _data;
volatile bool[] _isDataLoaded.
I can see two obvious sources of threading problems
_dataand_isDataLoaded? If these types are not thread safe then neither is your code arrays are only thread safe if no two threads access the same element, which could happen in the above code. Try using a ConcurrentDictionary instead.GetCachedConstantandCacheToFilethere is a potential race condition whereby thread A could begin writing cached data to a file causing thread B to go down theFile.Existsroute when in fact the file only contains partially written dataI’d guess the culprit is the first of these two – there might be other problems but unless these types are thread safe no amount of double checked locking will save you unless you also synchronize access to those objects.