Queue.SyncRoot Propriedade
Definição
public:
virtual property System::Object ^ SyncRoot { System::Object ^ get(); };
public virtual object SyncRoot { get; }
member this.SyncRoot : obj
Public Overridable ReadOnly Property SyncRoot As Object
Valor da propriedade
Um objeto que pode ser usado para sincronizar o acesso à Queue.An object that can be used to synchronize access to the Queue.
Implementações
Exemplos
O exemplo de código a seguir mostra como bloquear a coleção usando o SyncRoot durante toda a enumeração.The following code example shows how to lock the collection using the SyncRoot during the entire enumeration. A recuperação do valor dessa propriedade é uma O(1) operação.Retrieving the value of this property is an O(1) operation.
Queue^ myCollection = gcnew Queue();
bool lockTaken = false;
try
{
Monitor::Enter(myCollection->SyncRoot, lockTaken);
for each (Object^ item in myCollection);
{
// Insert your code here.
}
}
finally
{
if (lockTaken)
{
Monitor::Exit(myCollection->SyncRoot);
}
}
Queue myCollection = new Queue();
lock(myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New Queue()
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
Comentários
Para criar uma versão sincronizada do Queue , use o Synchronized método.To create a synchronized version of the Queue, use the Synchronized method. No entanto, as classes derivadas podem fornecer sua própria versão sincronizada do Queue usando a SyncRoot propriedade.However, derived classes can provide their own synchronized version of the Queue using the SyncRoot property. O código de sincronização deve executar operações no SyncRoot do Queue , não diretamente no Queue .The synchronizing code must perform operations on the SyncRoot of the Queue, not directly on the Queue. Isso garante a operação apropriada das coleções que são derivadas de outros objetos.This ensures proper operation of collections that are derived from other objects. Especificamente, ele mantém a sincronização adequada com outros threads que podem estar modificando o Queue objeto simultaneamente.Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the Queue object.
A enumeração por meio de uma coleção não é um procedimento thread-safe intrínseco.Enumerating through a collection is intrinsically not a thread-safe procedure. Mesmo quando uma coleção está sincronizada, outros threads ainda podem modificar a coleção, o que faz o enumerador lançar uma exceção.Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. Para garantir thread-safe durante a enumeração, é possível bloquear a coleção durante toda a enumeração ou verificar as exceções resultantes das alterações feitas por outros threads.To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.