What is the use of Thread.BeginThreadAffinity()
method in Thread class ?
It will be helpful for me to understand if you provide its sample example with exaplaination.
Thanks.
In .NET 1.x, a Thread was always matched with an operating system thread. At the request of the SQL Server team, that association was broken for .NET 2.0. A CLR host can now take control of thread mapping itself, the IHostTaskManager is the work-horse interface for that. There's a good backgrounder in this blog post.
Sometimes code really does care that it runs on a particular operating system thread. Windows critical sections and mutants would be an example. Really, any kind of unmanaged code interop. Thread.BeginThreadAffinity() invokes IHostTaskManager::BeginThreadAffinity() to let the host know that the task should not be allowed to run on another operating system thread but stick on the one it is currently on, until EndThreadAffinity() is called.
But, don't worry about any of this. The SQL Server project was a bust, they couldn't get it reliable. There have been no signs that they'll try again.
Did you read the remarks section of the documentation . It explains it pretty well IMO.
Some hosts of the common language runtime, such as Microsoft SQL Server 2005, provide their own thread management. A host that provides its own thread management can move an executing task from one physical operating system thread to another at any time. Most tasks are not affected by this switching. However, some tasks have thread affinity - that is, they depend on the identity of a physical operating system thread. These tasks must inform the host when they execute code that should not be switched.
For example, if your application calls a system API to acquire an operating system lock that has thread affinity, such as a Win32 CRITICAL_SECTION, you must call BeginThreadAffinity before acquiring the lock, and EndThreadAffinity after releasing the lock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With