0

I'd implemented threadsafe dataset update operation in a windows console application as given in the answer of this question.

Threadsafe Dataset

I want to double check the approach that I'm following for updating the dataset row.

Each thread has UpdateData instance method. This method creates an string array of primary keys to find the particular row in datatable and Dictionary of data to be updated in that row.

private void UpdateData()
{

string[] key=new string(){"",""};
Dictionary<string,string> data=new Dictionary<string,string>();

key[0]="key1";
key[1]="key2";
data.Add("col1","value1");
data.Add("col2","value2");
MyDataSet.UpdateRecord(key,data);
}

Now there is this shared method used to update the DataTable Row in global DataSet.

public sealed class MyDataSet
{

public static DataSet ds= new DataSet();

private static object _lock =new object();

public static UpdateRow(string[] key,Dictionary<string,string> data)
{
  lock(_lock){

    DataRow dr=ds.Tables[0].Rows.Find(key);
    dr.AcceptChanges();
    dr.BeginEdit();
    foreach(KeyValuePair<string,string> field in data)
     {
       dr[field.key]=field.value;
     }

    dr.EndEdit();
  }
}
}

So my question is if this approach of passing the key and data which are of reference type to a static shared method impose any problems to thread safety? Both key and data are not shared and each thread creates its own copy. If two threads enter the UpdateRow method, will it be possible by any means to update row using key passed by one thread and data of another thread (I know this questions sounds stupid )

1 Answer 1

2

If the UpdateRow method is called by two different threads there will be two different stacks created and each stack will hold the references to the parameters passed in. So in each executing context you are guaranteed that all the parameters will be consistent.

So, no it is not possible to update with the key from one and data from another stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.