Esse uso de uma fila estática é seguro para threads?
A documentação do msdn afirma que uma Fila genérica estática é segura para threads. Isso significa que o código a seguir é seguro para threads? Em outras palavras, existe um problema quando um thread enfileira um int e outro thread enfileira um int ao mesmo tempo? Preciso bloquear as operações de enfileiramento e desenfileiramento para segurança da thread?
class Test {
public static Queue<int> queue = new Queue<int>(10000);
Thread putIntThread;
Thread takeIntThread;
public Test() {
for(int i = 0; i < 5000; ++i) {
queue.Enqueue(0);
}
putIntThread = new Thread(this.PutInt);
takeIntThread = new Thread(this.TakeInt);
putIntThread.Start();
takeIntThread.Start();
}
void PutInt() {
while(true)
{
if(queue.Count < 10000) {//no need to lock here as only itself can change this condition
queue.Enqueue(0);
}
}
}
void TakeInt() {
while(true) {
if(queue.Count > 0) {//no need to lock here as only itself can change this condition
queue.Dequeue();
}
}
}
}
Edit: Eu tenho que usar o .NET 3.5