Chapter 14. Scenarios

In this chapter, we’ll take a look at a variety of types and techniques to address some common scenarios when writing concurrent programs. These kinds of scenarios could fill up another entire book, so I’ve selected just a few that I’ve found the most useful.

14.1 Initializing Shared Resources

Problem

You have a resource that is shared between multiple parts of your code. This resource needs to be initialized the first time it is accessed.

Solution

The .NET framework includes a type specifically for this purpose: Lazy<T>. You construct an instance of the Lazy<T> type with a factory delegate that is used to initialize the instance. The instance is then made available via the Value property. The following code illustrates the Lazy<T> type:

static int _simpleValue;
static readonly Lazy<int> MySharedInteger = new Lazy<int>(() => _simpleValue++);

void UseSharedInteger()
{
  int sharedValue = MySharedInteger.Value;
}

No matter how many threads call UseSharedInteger simultaneously, the factory delegate is only executed once, and all threads wait for the same instance. Once it’s created, the instance is cached and all future access to the Value property returns the same instance (in the preceding example, MySharedInteger.Value will always be 0).

A very similar approach can be used if the initialization requires asynchronous work; in this case, you can use a Lazy<Task<T>>:

static int _simpleValue;
static readonly Lazy<Task<int>> MySharedAsyncInteger =
    new Lazy

Get Concurrency in C# Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.