title: How do you check if a calling thread has thread affinity turned on?
body: <p>If I were to build a library, I might have a static function that does a <code>Thread.BeginThreadAffinity()</code> at the start of the function and <code>Thread.EndThreadAffinity()</code> at the end, or in the finalizer, of the function because I want to enforce a 1-to-1 mapping of managed to OS (native) threads:</p>
  
  <pre><code>public static void DoSomething()
  {
      Thread.BeginThreadAffinity()
      // Do something, like a few P/Invokes that would require me to have thread affinity.
      Thread.EndThreadAffinity()
  }
  </code></pre>
  
  <p>The issue is this: If the caller already has thread affinity on their thread before calling <code>DoSomething()</code>, I might want to check that thread affinity is already on the current thread, so that in the finalizer of my function, I don't end thread affinity for the caller. So, is there any way to check if the calling thread has enabled affinity? If not, I might have to spawn a new thread for each call to this static function and wait on an AutoResetEvent.</p>