Monday, 2 September 2013

C#, .Net - Is Locking this instance method required

C#, .Net - Is Locking this instance method required

For the question that I have probably I need more convincing answer to
implement in my solution. I am not sure whether my understanding is
correct. Following are the implementation details:
In a class, in the main method, where the class object C is created to
call an instance method that takes integer as a parameter:
public <AnyClass> MyMethod(int classVar)
{
return new <AnyClass>(classVar);
// Can have more implementation, using the parameter passed
}
In the main, I want this method to be called on the multiple threads,
using the same class object, the parameter would be the value supplied by
for loop that starts the threads to execute. Now in memory we are
executing same method, does this needs any kind of locking. In my view No,
I have tested, but I am not sure in theory why, wouldn't different threads
mess up with each other's parameter value, in my implementation it doesn't
seems the case.
Only thing I cannot guarantee, is which thread access and returns first as
that would not be in the order, but if I do not care about it, is this
implementation correct.
Please note, this is an attempt to describe the issue in a stand alone
manner, I have similar implementation as part of a complex project.
Any suggestions / pointers would be great. Please let me know if you need
a clarification.
thanks,
Mrinal

No comments:

Post a Comment