Thread.AllocateDataSlot メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim returnValue As LocalDataStoreSlot returnValue = Thread.AllocateDataSlot
LocalDataStoreSlot。

![]() |
---|
このメソッドに適用される HostProtectionAttribute 属性の Resources プロパティの値は、SharedState または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (通常、アイコンのダブルクリック、コマンドの入力、またはブラウザへの URL の入力により起動されるもの) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
スレッドは、ローカル ストア メモリ機構を使用して、スレッド固有のデータを格納します。共通言語ランタイムは、各プロセスの作成時に、そのプロセスにマルチスロットのデータ ストア配列を割り当てます。スレッドは、データ ストアのデータ スロットを割り当てたり、スロットにデータ値を格納および取得したり、スレッドの有効期限が切れた後でスロットを再利用できるようにスロットを解放したりできます。データ スロットはスレッドごとに一意です。他のスレッドは (子スレッドであっても) そのデータを取得できません。

データ スロットにスレッド固有の情報を格納する方法の例を次に示します。
Imports System Imports System.Threading Class Test <MTAThread> _ Shared Sub Main() Dim newThreads(3) As Thread For i As Integer = 0 To newThreads.Length - 1 newThreads(i) = New Thread(AddressOf Slot.SlotTest) newThreads(i).Start() Next i End Sub End Class Public Class Slot Shared randomGenerator As Random Shared localSlot As LocalDataStoreSlot Shared Sub New() randomGenerator = new Random() localSlot = Thread.AllocateDataSlot() End Sub Shared Sub SlotTest() ' Set different data in each thread's data slot. Thread.SetData(localSlot, randomGenerator.Next(1, 200)) ' Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _ AppDomain.GetCurrentThreadId().ToString(), _ Thread.GetData(localSlot).ToString()) ' Allow other threads time to execute SetData to show ' that a thread's data slot is unique to the thread. Thread.Sleep(1000) ' Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _ AppDomain.GetCurrentThreadId().ToString(), _ Thread.GetData(localSlot).ToString()) End Sub End Class
using System; using System.Threading; class Test { static void Main() { Thread[] newThreads = new Thread[4]; for(int i = 0; i < newThreads.Length; i++) { newThreads[i] = new Thread( new ThreadStart(Slot.SlotTest)); newThreads[i].Start(); } } } class Slot { static Random randomGenerator; static LocalDataStoreSlot localSlot; static Slot() { randomGenerator = new Random(); localSlot = Thread.AllocateDataSlot(); } public static void SlotTest() { // Set different data in each thread's data slot. Thread.SetData(localSlot, randomGenerator.Next(1, 200)); // Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", AppDomain.GetCurrentThreadId().ToString(), Thread.GetData(localSlot).ToString()); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to the thread. Thread.Sleep(1000); Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", AppDomain.GetCurrentThreadId().ToString(), Thread.GetData(localSlot).ToString()); } }
using namespace System; using namespace System::Threading; ref class Slot { private: static Random^ randomGenerator; static LocalDataStoreSlot^ localSlot; static Slot() { randomGenerator = gcnew Random; localSlot = Thread::AllocateDataSlot(); } public: static void SlotTest() { // Set different data in each thread's data slot. Thread::SetData( localSlot, randomGenerator->Next( 1, 200 ) ); // Write the data from each thread's data slot. Console::WriteLine( "Data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( localSlot )->ToString() ); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to the thread. Thread::Sleep( 1000 ); Console::WriteLine( "Data in thread_{0}'s data slot: {1,3}", AppDomain::GetCurrentThreadId().ToString(), Thread::GetData( localSlot )->ToString() ); } }; int main() { array<Thread^>^newThreads = gcnew array<Thread^>(4); for ( int i = 0; i < newThreads->Length; i++ ) { newThreads[ i ] = gcnew Thread( gcnew ThreadStart( &Slot::SlotTest ) ); newThreads[ i ]->Start(); } }
import System.*; import System.Threading.*; import System.Threading.Thread; class Test { public static void main(String[] args) { Thread newThreads[] = new Thread[4]; for (int i = 0; i < newThreads.length; i++) { newThreads[i] = new Thread(new ThreadStart(Slot.SlotTest)); newThreads[i].Start(); } } //main } //Test class Slot { private static Random randomGenerator; private static LocalDataStoreSlot localSlot; static { randomGenerator = new Random(); localSlot = Thread.AllocateDataSlot(); } //static public static void SlotTest() { // Set different data in each thread's data slot. Thread.SetData(localSlot, new Integer(randomGenerator.Next(1, 200))); // Write the data from each thread's data slot. Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", String.valueOf(AppDomain.GetCurrentThreadId()), Thread.GetData(localSlot).toString()); // Allow other threads time to execute SetData to show // that a thread's data slot is unique to the thread. Thread.Sleep(1000); Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", String.valueOf(AppDomain.GetCurrentThreadId()), Thread.GetData(localSlot).toString()); } //SlotTest } //Slot

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からThread.AllocateDataSlot メソッドを検索する場合は、下記のリンクをクリックしてください。

- Thread.AllocateDataSlot メソッドのページへのリンク