Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Locking mechanism - write only locking

In continuation for my latest ponders about locks in C# and .NET,

Consider the following scenario:

I have a class which contains a specific collection (for this example, i've used a Dictionary<string, int>) which is updated from a data source every few minutes using a specific method which it's body you can see below:

    DataTable dataTable = dbClient.ExecuteDataSet(i_Query).GetFirstTable();

    lock (r_MappingLock)
    {
        i_MapObj.Clear();

        foreach (DataRow currRow in dataTable.Rows)
        {
            i_MapObj.Add(Convert.ToString(currRow[i_Column1]), Convert.ToInt32[i_Column2]));
        }
    }

r_MappingLock is an object dedicated to lock the critical section which refreshes the dictionary's contents.

i_MapObj is the dictionary object

i_Column1 and i_Column2 are the datatable's column names which contain the desired data for the mapping.

Now, I also have a class method which receives a string and returns the correct mapped int based on the mentioned dictionary.

I want this method to wait until the refresh method completes it's execution, so at first glance one would consider the following implementation:

    lock (r_MappingLock)
    {
        int? retVal = null;

        if (i_MapObj.ContainsKey(i_Key))
        {
            retVal = i_MapObj[i_Key];
        }

        return retVal;
    }

This will prevent unexpected behaviour and return value while the dictionary is being updated, but another issue arises: Since every thread which executes the above method tries to claim the lock, it means that if multiple threads try to execute this method at the same time, each will have to wait until the previous thread finished executing the method and try to claim the lock, and this is obviously an undesirable behaviour since the above method is only for reading purposes.

I was thinking of adding a boolean member to the class which will be set to true or false wether the dictionary is being updated or not and checking it within the "read only" method, but this arise other race-condition based issues...

Any ideas how to solve this gracefully?

Thanks again,

Mikey

like image 976
Mikey S. Avatar asked Aug 14 '11 19:08

Mikey S.


4 Answers

Have a look at the built in ReaderWriterLock.

like image 170
Albin Sunnanbo Avatar answered Oct 24 '22 18:10

Albin Sunnanbo


I would just switch to using a ConcurrentDictionary to avoid this situation altogether - manually locking is error-prone. Also as I can gather from "C#: The Curious ConcurrentDictionary", ConcurrentDictionary is already read-optimized.

like image 31
BrokenGlass Avatar answered Oct 24 '22 19:10

BrokenGlass


Albin pointed out correctly at ReaderWriterLock. I will add an even nicer one: ReaderWriterGate by Jeffrey Richter. Enjoy!

like image 2
Vladimir Avatar answered Oct 24 '22 20:10

Vladimir


You might consider creating a new dictionary when updating, instead of locking. This way, you will always have consistent results, but reads during updates would return previous data:

private volatile Dictionary<string, int> i_MapObj = new Dictionary<string, int>();

private void Update()
{
    DataTable dataTable = dbClient.ExecuteDataSet(i_Query).GetFirstTable();

    var newData = new Dictionary<string, int>();
    foreach (DataRow currRow in dataTable.Rows)
    {
        newData.Add(Convert.ToString(currRow[i_Column1]), Convert.ToInt32[i_Column2]));
    }

    // Start using new data - reference assignments are atomic
    i_MapObj = newData;
}

private int? GetValue(string key)
{
    int value;
    if (i_MapObj.TryGetValue(key, out value))
        return value;

    return null;
}
like image 1
Bojan Resnik Avatar answered Oct 24 '22 19:10

Bojan Resnik