1) Not always. If you have any Shared fields on the type and you are
both reading and writing them, then probably. Thread safety is needed
when you are sharing data between threads.
2) Threads are an abstraction that 'execute' code. Types are an
abstraction that 'contain' code. You can have multiple threads
executing the same code inside of a type concurrently. If you have
data that can be touched by multiple threads, you have to be careful
that two threads are not changing an ArrayList, for example, at the
same time, because it leads to unpredictable behavior and you spend a
lot of time debugging it and cursing at the computer.
SyncLock locks on a reference type. Think of it as setting a flag on
that reference type for the duration of the lock. When another thread
tries to take a lock on the same object, the runtime can see the flag
is set and force the second thread to wait for the lock to become
free.
Hope this made some sense..
--
Scott
http://www.OdeToCode.com
On Thu, 16 Sep 2004 00:19:55 +0200, "Henri" <hm********@hotmail.com>
wrote:
1) Do Shared methods always need to be synchronised to be thread-safe?
2) Why must SyncLock be used with a reference type? I can't get the relation
between threads and a type.
Thanks for your help.
Henri