if we have a ConcurrentBag<object>
safeBag` filled with 100 objects.
then one thread works as:
foreach(object o in safeBag)
{
Thread.Sleep(1000);
}
the other thread starts right after the 1st thread starts:
{
safeBag.AddOrTake(something);
}
Will the 2nd thread wait for 100Sec to enter the resource? Another question, if the 1st thread run with Parallel.ForEach(),how will the threads work?
EDIT:The MSDN said:"A List can support multiple readers concurrently, as long as the collection is not modified. Enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with one or more write accesses, the only way to ensure thread safety is to lock the collection during the entire enumeration." Does the Enumerating through the ConcurrentBag cause the 2nd thread waiting at writing access to the ConcurrentBag?