Threading and Concurrency

Reading to this point in this chapter, one might think that threads serve as little more than a place to do frame bookkeeping. While this aspect of threads is certainly important, the primary role of threads has always been to organize programming tasks into understandable units of concurrency and synchronization. We will now turn our attention to this more familiar facet of threading, and examine how programmers can use managed threads to build concurrent applications.

Threads are the primary abstraction for execution and concurrency in the CLI. Because the CLI includes support for concurrent execution, it must also allow independently executing parts of a program to communicate and to synchronize their state, as well as provide programmers with the ability to impact the scheduling of concurrent activity.

The execution engine provides mechanisms for all of these:

  • Programmers can communicate between component references that have been marshaled between threads using method calls.

  • Programmers can synchronize access to instance and static methods, instance fields, and arbitrary regions of their components, either automatically or manually.

  • Programmers can control thread scheduling by manipulating instances of the Thread component or by using the CLI threadpool, which is an efficient runtime service that simplifies worker thread creation and scheduling.

We will visit each of these mechanisms in turn; but first, we will examine how managed threads are implemented. ...

Get Shared Source CLI Essentials 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.