December 2019
Intermediate to advanced
346 pages
9h 8m
English
ConcurrentBag<T> is an unordered collection, unlike ConcurrentStack and ConcurrentQueues, which orders the items while storing and retrieving them. ConcurrentBag<T> is optimized for scenarios in which the same threads work as a producer as well as a consumer. ConcurrentBag supports the work-stealing algorithm and maintains a local queue for each thread.
The following code creates ConcurrentBag and adds or gets items from it:
ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();//Add item to bagconcurrentBag.Add(10);int item;//Getting items from BagconcurrentBag.TryTake(out item)
The complete code is as follows:
static ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();private static void ConcurrentBackDemo() ...
Read now
Unlock full access