Open sandboxFocus

Class ThreadUnsafeAttribute

Custom attribute that, when applied to a type, ensures that only one thread executes in methods of this type. When more than one thread accesses methods of this type, a ConcurrentAccessException is thrown.

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

Use this model for objects that must never be accessed concurrently, to detect violations deterministically instead of corrupting the object. Public and internal methods acquire exclusive access to the object, like in the SynchronizedAttribute model, but a thread that cannot acquire access immediately throws a ConcurrentAccessException instead of waiting. Accessing a field from a thread that has not acquired access results in a ThreadAccessException.

These checks are run-time verifications: they are performed only when RuntimeVerificationEnabled is true, which is the default in debug builds only. A build-time error is emitted when the class has public or internal fields.

Constructors

Name Description
ThreadUnsafeAttribute()

Initializes a new instance of the ThreadUnsafeAttribute class.

ThreadUnsafeAttribute(PortableFormatterConstructorContext)

See Also