Thread.BeginThreadAffinity メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Thread.BeginThreadAffinity メソッドの意味・解説 

Thread.BeginThreadAffinity メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

マネージ コードが現在のオペレーティング システムの物理スレッドの ID に依存する命令の実行を開始することをホストに通知します。

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Sub BeginThreadAffinity
Thread.BeginThreadAffinity
public static void BeginThreadAffinity
 ()
public:
static void BeginThreadAffinity ()
public static void BeginThreadAffinity
 ()
public static function BeginThreadAffinity
 ()
例外例外
解説解説

共通言語ランタイムの一部のホスト (たとえば Microsoft® SQL Server "Yukon") では、独自のスレッド管理機能が用意されています。独自のスレッド管理機能を持つホストは、いつでも実行タスクをオペレーティング システムの物理スレッド間で移動できます。ほとんどのタスクはこの切り替えの影響を受けません。ただし、一部のタスクにはスレッド アフィニティがあります。つまり、これらのタスクは、オペレーティング システムの物理スレッドの ID に依存します。これらのタスクは、切り替えてはいけないコードを実行するときにホストに通知する必要があります。

たとえば、Win32 CRITICAL_SECTION のようなスレッド アフィニティを持つオペレーティング システムのロックを取得するシステム API をアプリケーションで呼び出す場合、このロックを取得する前に BeginThreadAffinity を呼び出し、ロックを解放した後に EndThreadAffinity を呼び出す必要があります。

また、WaitHandle から継承する .NET Framework の型はオペレーティング システム オブジェクトに依存するため、これらの型をブロックする前に BeginThreadAffinity を呼び出す必要があります。

SQL Server "Yukon" で実行するコード内でこのメソッドを使用するには、ホストの保護レベルを最大にしてコードを実行する必要があります。

使用例使用例

BeginThreadAffinity メソッドおよび EndThreadAffinity メソッドを使用してコード ブロックがオペレーティング システムの物理スレッドの ID に依存することをホストに通知する例を次に示します。

Imports System.Threading
Imports System.Security.Permissions

<SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)>
 _
Friend Class MyUtility
    <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlThread)>
 _
    Public Sub PerformTask() 
        ' Code that does not have thread affinity goes here.
        '
        Thread.BeginThreadAffinity()
        '
        ' Code that has thread affinity goes here.
        '
        Thread.EndThreadAffinity()
        '
        ' More code that does not have thread affinity.
    End Sub 'PerformTask
End Class 'MyUtility 
using System.Threading;
using System.Security.Permissions;

[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlThread)]
public class MyUtility
{
    [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlThread)]
    public void PerformTask()
    {
        // Code that does not have thread affinity goes here.
        //
        Thread.BeginThreadAffinity();
        //
        // Code that has thread affinity goes here.
        //
        Thread.EndThreadAffinity();
        //
        // More code that does not have thread affinity.
    }
}
using namespace System::Threading;
using namespace System::Security::Permissions;

public ref class MyUtility
{
public:
   [SecurityPermissionAttribute(SecurityAction::Demand, ControlThread=true)]
   void PerformTask()
   {
      // Code that does not have thread affinity goes here.
      //
      Thread::BeginThreadAffinity();
      //
      // Code that has thread affinity goes here.
      //
      Thread::EndThreadAffinity();
      //
      // More code that does not have thread affinity.
   }
};
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Thread.BeginThreadAffinity メソッド」の関連用語

Thread.BeginThreadAffinity メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Thread.BeginThreadAffinity メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS