Multithreading in C#
Objectives
To understand the notion of multithreading.
To appreciate how multithreading can improve program performance.
To understand how to create, manage and destroy threads.
To understand the life cycle of a thread.
To understand thread priorities and scheduling.
The spider's touch, how exquisitely fine! Feels at each thread, and
lives along the line.
Alexander Pope
A person with one watch knows what time it is; a person with two watches
is never sure.
Proverb
Learn to labor and to wait.
Henry Wadsworth Longfellow
The most general definition of beauty...Multeity in Unity.
Samuel Taylor Coleridge
Outline
12.1 |
Introduction |
12.2 |
Thread States: Life Cycle of a Thread |
12.3 |
Thread Priorities and Thread Scheduling |
12.4 |
Summary |
12.1 Introduction
It would be nice if we could perform one action at a time and perform it well, but that is usually difficult to do. The human body performs a great variety of operations in parallelor, as we will say throughout this chapter, concurrently R . espiration, blood circulation and digestion, for example, can occur concurrently. All the sensessight, touch, smell, taste and hearingcan occur at once. Computers, too, perform operations concurrently. It is common for desktop personal computers to be compiling a program, sending a file to a printer and receiving electronic mail messages over a network concurrently.
Ironically, most programming languages do not enable programmers to specify concurrent activities. Rather, programming languages generally provide only a simple set of control structures that enable programmers to perform one action at a time, proceeding to the next action after the previous one has finished. Historically, the type of concurrency that computers perform today generally has been implemented as operating system "primitives" available only to highly experienced "systems programmers." The Ada programming language, developed by the United States Department of Defense, made concurrency primitives widely available to defense contractors building military command-and-control systems. However, Ada has not been widely used in universities and commercial industry.
The .NET Framework Class Library makes concurrency primitives available to the applications programmer. The programmer specifies that applications contain "threads of execution," each thread designating a portion of a program that may execute concurrently with other threadsthis capability is called multithreading. Multithreading is available to all .NET programming languages, including C#, Visual Basic and Visual C++.
Software Engineering Observation 12.1
The .NET Framework Class Library includes multithreading capabilities in namespace multithreading among a larger part of the applications-programming community. 12.1
We discuss many applications of concurrent programming. When programs download large files, such as audio clips or video clips from the World Wide Web, users do not want to wait until an entire clip downloads before starting the playback. To solve this problem, we can put multiple threads to workone thread downloads a clip, and another plays the clip. These activities, or tasks, then may proceed concurrently. To avoid choppy playback, we synchronize the threads so that the player thread does not begin until there is a sufficient amount of the clip in memory to keep the player thread busy.
Another example of multithreading is C#'s automatic garbage collection. C and C++ place with the programmer the responsibility of reclaiming dynamically allocated memory.
C# provides a garbage-collector thread that reclaims dynamically allocated memory that is no longer needed.
Performance Tip 12.1
One of the reasons for the popularity of C and C++ over the years was that their memory-management techniques were more efficient than those of languages that used g*arbage collectors. In fact, memory management in C# often is faster than in C or C++.1 12.1
Good Programming Practice 12.1
See an object reference to null when the program no longer needs that object. This enables possible moment that the object can be garbage collected. If such an object has other references to it, that object cannot be collected.12.1
Writing multithreaded programs can be tricky. Although the human mind can perform functions concurrently, people find it difficult to jump between parallel "trains of thought." To see why multithreading can be difficult to program and understand, try the following experiment: Open three books to page 1 and try reading the books concurrently. Read a few words from the first book, then read a few words from the second book, then read a few words from the third book, then loop back and read the next few words from the first book, etc. After this experiment, you will appreciate the challenges of multithreadingswitching between books, reading briefly, remembering your place in each book, moving the book you are reading closer so you can see it, pushing books you are not reading asideand amidst all this chaos, trying to comprehend the content of the books!
Performance Tip 12.2
A problem with single-threaded applications is that lengthy activities must complete before other activities can begin. In a multithreaded application, threads can share a processor (or set of processors), so that multiple tasks are performed in parallel. 12.2