Open sandboxFocus

Class SynchronizedAttribute

Custom attribute that, when applied to a class, applies the SynchronizedThreadingModel to this class. In a synchronized object, only a single thread can access the object at a time. If a second thread needs to access the object while it is in use by a first thread, the second thread has to wait until the first thread completes its access.

Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
Syntax
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.Synchronized")]
[Serializer(typeof(SynchronizedAttribute.Serializer))]
public sealed class SynchronizedAttribute : ThreadAwareAttribute, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, IAspectProvider, IService, IAdviceProvider, IAspect, IThreadAware, IQueryInterface
Remarks

The model is similar to enclosing each public and internal method in a lock statement, with the following differences: the lock is not taken on the object itself, it cannot be forgotten, and it extends to the child objects of the object.

A build-time error is emitted when the class has public or internal fields, or when an async method is not annotated with the ReentrantAttribute custom attribute. When run-time verification is enabled, accessing a field from a thread that does not hold the lock results in a ThreadAccessException. This typically happens in a private callback method, such as an event handler; add the EntryPointAttribute custom attribute to such methods.

Constructors

Name Description
SynchronizedAttribute()
SynchronizedAttribute(PortableFormatterConstructorContext)

See Also