// This context-bound type will only be loaded into a
// synchronized (hence thread-safe) context.
[Synchronization]
class SportsCarTS : ContextBoundObject
{}
Types that are attributed with the [Synchronization] attribute are loaded into a thread-safe
context. Given the special contextual needs of the MyThreadSafeObject class type, imagine the
problems that would occur if an allocated object were moved from a synchronized context into a
nonsynchronized context. The object is suddenly no longer thread safe and thus becomes a candi-
date for massive data corruption, as numerous threads are attempting to interact with the (now
thread-volatile) reference object. To ensure the CLR does not move SportsCarTS objects outside of
a synchronized context, simply derive from ContextBoundObject.
Do'stlaringiz bilan baham: |