Thread.FreeNamedDataSlot(String) Metoda

Definice

Eliminuje přidružení mezi názvem a slotem pro všechna vlákna v procesu.Eliminates the association between a name and a slot, for all threads in the process. Pro lepší výkon použijte pole, která jsou označena atributem ThreadStaticAttribute.For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead.

public:
 static void FreeNamedDataSlot(System::String ^ name);
public static void FreeNamedDataSlot (string name);
static member FreeNamedDataSlot : string -> unit
Public Shared Sub FreeNamedDataSlot (name As String)

Parametry

name
String

Název datové přihrádky, která se má uvolnitThe name of the data slot to be freed.

Příklady

Tato část obsahuje dva příklady kódu.This section contains two code examples. První příklad ukazuje, jak použít pole, které je označeno atributem ThreadStaticAttribute pro uchovávání informací specifických pro vlákno.The first example shows how to use a field that is marked with the ThreadStaticAttribute attribute to hold thread-specific information. Druhý příklad ukazuje, jak použít datovou datovou oblast ke stejnému účelu.The second example shows how to use a data slot to do the same thing.

První příkladFirst Example

Následující příklad ukazuje, jak použít pole, které je označeno ThreadStaticAttribute pro uchovávání informací specifických pro vlákno.The following example shows how to use a field that is marked with ThreadStaticAttribute to hold thread-specific information. Tento postup poskytuje lepší výkon než metoda, která je uvedena v druhém příkladu.This technique provides better performance than the technique that is shown in the second example.

using namespace System;
using namespace System::Threading;

ref class ThreadData
{
private:
   [ThreadStatic]
   static int threadSpecificData;

public:
   static void ThreadStaticDemo()
   {
      // Store the managed thread id for each thread in the static
      // variable.
      threadSpecificData = Thread::CurrentThread->ManagedThreadId;
      
      // Allow other threads time to execute the same code, to show
      // that the static data is unique to each thread.
      Thread::Sleep( 1000 );

      // Display the static data.
      Console::WriteLine( "Data for managed thread {0}: {1}", 
         Thread::CurrentThread->ManagedThreadId, threadSpecificData );
   }
};

int main()
{
   for ( int i = 0; i < 3; i++ )
   {
      Thread^ newThread = 
          gcnew Thread( gcnew ThreadStart( ThreadData::ThreadStaticDemo )); 
      newThread->Start();
   }
}

/* This code example produces output similar to the following:

Data for managed thread 4: 4
Data for managed thread 5: 5
Data for managed thread 3: 3
 */
using System;
using System.Threading;

class Test
{
    static void Main()
    {
        for(int i = 0; i < 3; i++)
        {
            Thread newThread = new Thread(ThreadData.ThreadStaticDemo);
            newThread.Start();
        }
    }
}

class ThreadData
{
    [ThreadStatic]
    static int threadSpecificData;

    public static void ThreadStaticDemo()
    {
        // Store the managed thread id for each thread in the static
        // variable.
        threadSpecificData = Thread.CurrentThread.ManagedThreadId;
      
        // Allow other threads time to execute the same code, to show
        // that the static data is unique to each thread.
        Thread.Sleep( 1000 );

        // Display the static data.
        Console.WriteLine( "Data for managed thread {0}: {1}", 
            Thread.CurrentThread.ManagedThreadId, threadSpecificData );
    }
}

/* This code example produces output similar to the following:

Data for managed thread 4: 4
Data for managed thread 5: 5
Data for managed thread 3: 3
 */
Imports System.Threading

Class Test

    <MTAThread> _
    Shared Sub Main()

        For i As Integer = 1 To 3
            Dim newThread As New Thread(AddressOf ThreadData.ThreadStaticDemo)
            newThread.Start()
        Next i

    End Sub

End Class

Class ThreadData

    <ThreadStatic> _
    Shared threadSpecificData As Integer

    Shared Sub ThreadStaticDemo()

        ' Store the managed thread id for each thread in the static
        ' variable.
        threadSpecificData = Thread.CurrentThread.ManagedThreadId
      
        ' Allow other threads time to execute the same code, to show
        ' that the static data is unique to each thread.
        Thread.Sleep( 1000 )

        ' Display the static data.
        Console.WriteLine( "Data for managed thread {0}: {1}", _
            Thread.CurrentThread.ManagedThreadId, threadSpecificData )

    End Sub

End Class

' This code example produces output similar to the following:
'
'Data for managed thread 4: 4
'Data for managed thread 5: 5
'Data for managed thread 3: 3

Druhý příkladSecond Example

Následující příklad ukazuje, jak použít pojmenovaný datový slot k uložení informací specifických pro vlákno.The following example demonstrates how to use a named data slot to store thread-specific information.

using namespace System;
using namespace System::Threading;

ref class Slot
{
private:
    static Random^ randomGenerator = gcnew Random();

public:
    static void SlotTest()
    {
        // Set random data in each thread's data slot.
        int slotData = randomGenerator->Next(1, 200);
        int threadId = Thread::CurrentThread->ManagedThreadId;

        Thread::SetData(
            Thread::GetNamedDataSlot("Random"),
            slotData);

        // Show what was saved in the thread's data slot.
        Console::WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
            threadId, slotData);

        // Allow other threads time to execute SetData to show
        // that a thread's data slot is unique to itself.
        Thread::Sleep(1000);

        int newSlotData =
            (int)Thread::GetData(Thread::GetNamedDataSlot("Random"));

        if (newSlotData == slotData)
        {
            Console::WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
                threadId, newSlotData);
        }
        else
        {
            Console::WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
                threadId, newSlotData);
        }
    }
};

ref class Test
{
public:
    static void Main()
    {
        array<Thread^>^ newThreads = gcnew array<Thread^>(4);
        int i;
        for (i = 0; i < newThreads->Length; i++)
        {
            newThreads[i] =
                gcnew Thread(gcnew ThreadStart(&Slot::SlotTest));
            newThreads[i]->Start();
        }
        Thread::Sleep(2000);
        for (i = 0; i < newThreads->Length; i++)
        {
            newThreads[i]->Join();
            Console::WriteLine("Thread_{0} finished.",
                newThreads[i]->ManagedThreadId);
        }
    }
};

int main()
{
    Test::Main();
}
using System;
using System.Threading;

class Test
{
    public static void Main()
    {
        Thread[] newThreads = new Thread[4];
        int i;
        for (i = 0; i < newThreads.Length; i++)
        {
            newThreads[i] =
                new Thread(new ThreadStart(Slot.SlotTest));
            newThreads[i].Start();
        }
        Thread.Sleep(2000);
        for (i = 0; i < newThreads.Length; i++)
        {
            newThreads[i].Join();
            Console.WriteLine("Thread_{0} finished.",
                newThreads[i].ManagedThreadId);
        }
    }
}

class Slot
{
    private static Random randomGenerator = new Random();

    public static void SlotTest()
    {
        // Set random data in each thread's data slot.
        int slotData = randomGenerator.Next(1, 200);
        int threadId = Thread.CurrentThread.ManagedThreadId;

        Thread.SetData(
            Thread.GetNamedDataSlot("Random"),
            slotData);

        // Show what was saved in the thread's data slot.
        Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
            threadId, slotData);

        // Allow other threads time to execute SetData to show
        // that a thread's data slot is unique to itself.
        Thread.Sleep(1000);

        int newSlotData =
            (int)Thread.GetData(Thread.GetNamedDataSlot("Random"));

        if (newSlotData == slotData)
        {
            Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
                threadId, newSlotData);
        }
        else
        {
            Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
                threadId, newSlotData);
        }
    }
}
Imports System.Threading

Class Test
    Public Shared Sub Main()
        Dim newThreads(3) As Thread
        Dim i As Integer
        For i = 0 To newThreads.Length - 1
            newThreads(i) = _
                New Thread(New ThreadStart(AddressOf Slot.SlotTest))
            newThreads(i).Start()
        Next i
        Thread.Sleep(2000)
        For i = 0 To newThreads.Length - 1
            newThreads(i).Join()
            Console.WriteLine("Thread_{0} finished.", _
                newThreads(i).ManagedThreadId)
        Next i
    End Sub
End Class

Class Slot
    Private Shared randomGenerator As New Random()

    Public Shared Sub SlotTest()
        ' Set random data in each thread's data slot.
        Dim slotData As Integer = randomGenerator.Next(1, 200)
        Dim threadId As Integer = Thread.CurrentThread.ManagedThreadId

        Thread.SetData(
            Thread.GetNamedDataSlot("Random"),
            slotData)

        ' Show what was saved in the thread's data slot.
        Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
            threadId, slotData)

        ' Allow other threads time to execute SetData to show
        ' that a thread's data slot is unique to itself.
        Thread.Sleep(1000)

        Dim newSlotData As Integer = _
            CType(Thread.GetData(Thread.GetNamedDataSlot("Random")), Integer)

        If newSlotData = slotData Then
            Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
                threadId, newSlotData)
        Else
            Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
                threadId, newSlotData)
        End If
    End Sub
End Class

Poznámky

Důležité

.NET Framework poskytuje dva mechanismy pro použití úložiště thread local (TLS): statická pole v závislosti na vláknech (to znamená pole, která jsou označena atributem ThreadStaticAttribute) a datové sloty.The .NET Framework provides two mechanisms for using thread local storage (TLS): thread-relative static fields (that is, fields that are marked with the ThreadStaticAttribute attribute) and data slots. Statická pole v závislosti na vláknech poskytují mnohem lepší výkon než datové sloty a umožňují kontrolu typu v době kompilace.Thread-relative static fields provide much better performance than data slots, and enable compile-time type checking. Další informace o použití TLS najdete v tématu vlákna v místním úložišti: statická pole a datové sloty související s vláknem.For more information about using TLS, see Thread Local Storage: Thread-Relative Static Fields and Data Slots.

Po každém volání FreeNamedDataSlotjakékoli vlákno, které volá GetNamedDataSlot se stejným názvem, přidělí novému slotu přidruženému k názvu.After any thread calls FreeNamedDataSlot, any other thread that calls GetNamedDataSlot with the same name will allocate a new slot associated with the name. Následná volání GetNamedDataSlot jakýmkoli vláknem vrátí novou pozici.Subsequent calls to GetNamedDataSlot by any thread will return the new slot. Nicméně jakékoli vlákno, které stále obsahuje System.LocalDataStoreSlot vrácené starším voláním GetNamedDataSlot, může pokračovat v používání staré přihrádky.However, any thread that still has a System.LocalDataStoreSlot returned by an earlier call to GetNamedDataSlot can continue to use the old slot.

Slot, který je přidružený k názvu, se uvolní jenom v případě, že se uvolnila všechna LocalDataStoreSlot získaná před voláním FreeNamedDataSlot a uvolňováním paměti.A slot that has been associated with a name is released only when every LocalDataStoreSlot that was obtained prior to the call to FreeNamedDataSlot has been released and garbage-collected.

Vlákna používají mechanismus paměti místního úložiště k ukládání dat specifických pro vlákno.Threads use a local store memory mechanism to store thread-specific data. Modul CLR (Common Language Runtime) přidělí každému procesu při jeho vytvoření pole úložiště dat s více sloty.The common language runtime allocates a multi-slot data store array to each process when it is created. Vlákno může přidělovat datovou patici v úložišti dat, ukládat a načítat datovou hodnotu ve slotu a uvolnit slot pro opakované použití po vypršení platnosti vlákna.The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Datové sloty jsou jedinečné pro každé vlákno.Data slots are unique per thread. Žádná jiná vlákna (ne i podřízené vlákno) může získat tato data.No other thread (not even a child thread) can get that data.

Platí pro

Viz také