Reputation: 11293
Are the internals of a delegate to the Task Parallel Library thread safe - i.e. is that value of a variable declared within that delegate isolated to that thread?
Example:
Parallel.ForEach(collection, item => {
var something = new Something(item.Property);
});
Is something
guaranteed to always be a unique to a thread, protected against being overwritten by another thread created by the same Parallel
operation?
Upvotes: 3
Views: 2691
Reputation: 160882
Since this is a delegate something
is a local variable and certainly thread-safe if it is of a value type - of course it can still be overwritten if the variable holds a reference to a shared reference object (e.g. a reference to a variable that the delegate uses as a closure)
Upvotes: 5