A common platform for concurrent bags, stacks and queues in .NET

We’ve looked at the available concurrent collections in .NET before:

3 of these objects implement the same interface. Can you guess which three are similar in some sense? Stacks, bags and queues differ from dictionaries in that elements in those collections cannot be retrieved by an index of any sort. You can take/pop/dequeue the elements one by one but you cannot get to element #3 without first removing all elements before that.

Concurrent stack, bags and queues are first-choice solutions in consumer-producer applications – or producer-consumer, whichever direction you prefer. In those scenarios one or more threads put elements into a container, typically a collection. Those threads are the producers. One or more other threads then read from that collection and do whatever they want with the retrieved items. The items are also removed from the collection which is common for producer-consumer collections.

Think of messaging applications where one or more apps send messages to a message queue. The message queues have one or more consumers. They can also be called listeners or subscribers.

Normally the most natural option for such a scenario is a concurrent queue. You’ll most often want to consume the items in the order they have been put into the collection. However if you’re not sure which concurrent collection you’d like to go for there’s a relatively easy way to switch between the implementations.

Concurrent bags, queues and stacks implement a common interface: IProducerConsumerCollection of T. It doesn’t have any Peek method but that’s very rarely needed anyway. The TryAdd method adds an element to an IProducerConsumerCollection and returns false if the insertion has failed for whatever reason – which is very unlikely to ever occur. TryTake will try to take an element from the collection and returns true if it succeeded. In that case the element is returned as an “out” parameter.

Here’s an example which does the same as the code examples in the articles about bags, queues and stacks referenced above. We use the concurrent queue implementation for the interface but you can just replace it with ConcurrentStack and ConcurrentBag without any further modification:

public class ProducerConsumerInterfaceSampleService
	private IProducerConsumerCollection<int> _integerStack = new ConcurrentQueue<int>();
	public void RunConcurrentStackSample()
		Task readerOne = Task.Run(() => GetFromStack());
		Task readerTwo = Task.Run(() => GetFromStack());
		Task readerThree = Task.Run(() => GetFromStack());
		Task readerFour = Task.Run(() => GetFromStack());
		Task.WaitAll(readerOne, readerTwo, readerThree, readerFour);

	private void FillUpStack(int max)
		for (int i = 0; i <= max; i++)

	private void GetFromStack()
		int res;
		bool success = _integerStack.TryTake(out res);
		while (success)
			success = _integerStack.TryTake(out res);


If you run this code you’ll see that the 1000 elements are retrieved from the concurrent collection like we saw in the post on concurrent queues.

View the list of posts on the Task Parallel Library here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: