I wonder if Linq extension methods are atomic? Or do I need to lock
any IEnumerable
object used across threads, before any sort of iteration?
Does declaring the variable as volatile
have any affect on this?
To sum up, which of the following is the best, thread safe, operation?
1- Without any locks:
IEnumerable<T> _objs = //...
var foo = _objs.FirstOrDefault(t => // some condition
2- Including lock statements:
IEnumerable<T> _objs = //...
lock(_objs)
{
var foo = _objs.FirstOrDefault(t => // some condition
}
3- Declaring variable as volatile:
volatile IEnumerable<T> _objs = //...
var foo = _objs.FirstOrDefault(t => // some condition
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…