Key Points
For a user who wants all access, be it instance methods and instance fields access to be synchronized, this is the easiest approach. A type (class) is marked as context-bound and is attributed with the Synchronization custom attribute. The runtime with then allow only a single thread at any one time access to instance methods and fields.
[Synchronization()] public class Foo : ContextBoundObject { . . . // implementation for Foo } // Make an instance of Foo void SomeOtherMethod(void) { . . . Foo foo = new Foo(); }
When the object’s is called from outside the context synchronization occurs. If the call is synchronous then the call proceeds on the same thread. If the call is asynchronous then the message is queued to the thread pool and will be picked up by thread in the thread pool.
The Synchronization context property allows only a single thread to be active within the context at any one time. Multiple threads are able to enter the context but only one at a time. While a thread is active in the context, messages posted to the objects within the context will be added to the queue. This may be employed by one or more contexts to enforce a synchronization domain in which only one thread can execute at a time.
Synchronous calls are dispatched on the calling thread, which wait if the domain is occupied. This is done by contributing sinks that intercept and serialize in-coming calls for the respective contexts.
Asynchronous calls are dispatched using threads from the thread pool. The calling thread is returned immediately. Async methods use the Asynchronous Programming model mechanism that is described in Asynchronous Programming model specification.
By adding an instance of Synchronization to one or more contexts and obtaining proxies to objects living in these contexts, several threads can make Synchronous and Asynchronous calls on these objects. All the calls on these objects will be serialized due to the Synchronized context property.
If the synchronization property is marked for reentrancy, then call-outs are intercepted too. The call-out interception allows other waiting threads to enter the synchronization domain for maximal throughput.
[If reentrancy is false then no reentrancy is allowed even on the same physical thread
If reentrancy is true then all reentrancy is allowed and the context will service other calls during a call out.]