I mean for some basic operations, like reads/writes of class attributes. Or, maybe, it introduces some higher level synchronization?
views:
99answers:
2
+1
Q:
.NET: Does CLR automatically introduce basic thread-safety (locks) for heap allocated objects?
+1
A:
No it doesn't. You have to synchronize access to objects yourself. Individual reads/writes of 32 bit values on a 32 bit platform are atomic, but that doesn't mean that updates to an object are thread safe.
Since synchronization adds some overhead, adding this for all object would likely be a significant penalty for the overall performance.
Brian Rasmussen
2010-02-14 20:59:30
+3
A:
No, thread-safety is not guaranteed. You can use the lock
keyword on reference type instances (simple mutex functionality), for example. The language specification defines which operations are atomic without locks.
The new .NET 4 now includes thread-safe classes like ConcurrentBag that should make it easiert to write code without having to care about locks yourself.
AndiDog
2010-02-14 21:14:32