ConcurrentQueue<T> holding on to a few dequeued elements

Since .NET 4’s release, I’ve received several questions about a peculiar behavior of ConcurrentQueue<T> having to do with memory management. With Queue<T>, List<T>, and other such data structures in the .NET Framework, when you remove an element from the collection, the collection internally wipes out its reference to the stored element, e.g. class MyQueue<T> {… Read more

Building Async Coordination Primitives, Part 7: AsyncReaderWriterLock

In my last past, we looked at building an AsyncLock in terms of an AsyncSemaphore.  In this post, we’ll build a more advanced construct, an asynchronous reader/writer lock. An asynchronous reader/writer lock is more complicated than any of the previous coordination primitives we’ve created.  It also involves more policy, meaning there are more decisions to… Read more

Building Async Coordination Primitives, Part 6: AsyncLock

Last time, we looked at building an AsyncSemaphore.  Here, we’ll look at building support for an async mutual exclusion mechanism that supports scoping via ‘using’. As mentioned in the previous post, semaphores are great for throttling and resource management.  You can give a semaphore an initial count of the number of things to protect, and… Read more

Building Async Coordination Primitives, Part 5: AsyncSemaphore

In my last few posts, I covered building an AsyncManualResetEvent, an AsyncAutoResetEvent, an AsyncCountdownEvent, and an AsyncBarrier.  In this post, I’ll cover building an AsyncSemaphore class. Semaphores have a wide range of applicability.  They’re great for throttling, for protected access to a limited set of resources, and more.  There are two public semaphore types in… Read more

Building Async Coordination Primitives, Part 4: AsyncBarrier

Last time, we looked at building an AsyncCountdownEvent.  At the end of the post, I highlighted a common pattern for using such a type, which is for all of the participants to signal and then wait for all of the other participants to signal as well.  This kind of synchronization is typically referred to as… Read more

Building Async Coordination Primitives, Part 3: AsyncCountdownEvent

In my last two posts, I discussed building AsyncManualResetEvent and AsyncAutoResetEvent coordination primitives.  In this post, I’ll build on that to create a simple AsyncCountdownEvent. A countdown event is an event that will allow waiters to complete after receiving a particular number of signals.  The “countdown” comes from the common fork/join pattern in which it’s… Read more

Building Async Coordination Primitives, Part 2: AsyncAutoResetEvent

In my last post, I discussed building an asynchronous version of a manual-reset event.  This time, we’ll build an asynchronous version of an auto-reset event. A manual-reset event is transitioned to the signaled state when requested to do so (i.e. calling Set()), and then it remains in that state until it’s manually transitioned back to… Read more

Building Async Coordination Primitives, Part 1: AsyncManualResetEvent

The Task-based Async Pattern (TAP) isn’t just about asynchronous operations that you initiate and then asynchronously wait for to complete.  More generally, tasks can be used to represent all sorts of happenings, enabling you to await for any matter of condition to occur.  We can even use Tasks to build simple coordination primitives like their… Read more

Building a custom GetOrAdd method for ConcurrentDictionary<TKey,TValue>

I was recently asked by a developer about getting some additional information out of ConcurrentDictionary<TKey,TValue>’s GetOrAdd method.  As a reminder, GetOrAdd either returns the value for a key currently in the dictionary, or if that key doesn’t have a value, it adds a value for the key as dictated by either a TValue provided by… Read more

Await, SynchronizationContext, and Console Apps

When I discuss the new async language features of C# and Visual Basic, one of the attributes I ascribe to the await keyword is that it “tries to bring you back to where you were.” For example, if you use await on the UI thread of your WPF application, the code that comes after the… Read more