CA2003: Do not treat fibers as threads


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at CA2003: Do not treat fibers as threads.

|Breaking Change|Non-breaking|

A managed thread is being treated as a Win32 thread.

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.

Examine your usage of threads and change your code accordingly.

You should not suppress this rule.