For example is the following code thread safe:
ConcurrentQueue<Guid> _queue = new ConcurrentQueue<Guid>();
while(true)
{
for(int y = 0; y < 3; y++)
{
if(y % 3 == 0)
{
System.Threading.Tasks.Task.Run(() => _queue.Enqueue(Guid.NewGuid()));
}
else if (y % 3 == 1)
{
Guid x;
System.Threading.Tasks.Task.Run(() => _queue.TryDequeue(out x));
}
else if(y % 3 == 2)
{
System.Threading.Tasks.Task.Run(() =>
{
if (_queue.Any(t => t == testGuid))
{
// Do something
}
});
}
}
Edit: Apparently the title wasn't clear enough so updated the code sample to include actual multi threaded behaviour, yes the code above is just a sample of multi-threaded behaviour.