Share via


CA2003: Do not treat fibers as threads

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Item Value
TypeName DoNotTreatFibersAsThreads
CheckId CA2003
Category Microsoft.Reliability
Breaking Change Non-breaking

Cause

A managed thread is being treated as a Win32 thread.

Rule Description

Do not assume a managed thread is a Win32 thread. It is a fiber. The common language runtime (CLR) will run managed threads as fibers in the context of real threads that are owned by SQL. These threads can be shared across AppDomains and even databases in the SQL Server process. Using managed thread local storage will work, but you may not use unmanaged thread local storage or assume that your code will run on the current OS thread again. Do not change settings such as the locale of the thread. Do not call CreateCriticalSection or CreateMutex via P/Invoke because they require that the thread that enters a lock must also exit the lock. Because this will not be the case when you use fibers, Win32 critical sections and mutexes will be useless in SQL. You may safely use most of the state on a managed System.Thread object. This includes managed thread local storage and the current user interface (UI) culture of the thread. However, for programming model reasons, you will not be able to change the current culture of a thread when you use SQL; this will be enforced through a new permission.

How to Fix Violations

Examine your usage of threads and change your code accordingly.

When to Suppress Warnings

You should not suppress this rule.