C# thread safety for class instances
Posted
by
Steveng
on Stack Overflow
See other posts from Stack Overflow
or by Steveng
Published on 2011-01-10T09:32:12Z
Indexed on
2011/01/10
9:53 UTC
Read the original article
Hit count: 148
c#
|multithreading
I am learning C# and I am confused with the thread safety of the copies of the class instances as below:
eg:
classA objA;
classA objB = objA;
objA.field1 = value2; //do I need lock around modification of field1?
//let say we pass the objB to another thread
objB.field1 = value1 //do I need a lock for objB because of the modification of field1?
I am confused because coming from the background of C++, the class in C# is the reference type. If both objA and objB refer to the same memory underlying, then I would need a lock to protect the simultaneous writing to the field1. Could someone confirm with this or am I missing something?
Thanks.
© Stack Overflow or respective owner