Joseph Albahari, O'Reilly Media, Inc. All rights reserved. www. opvibpaberland.tk 1. Threading in C#. Joseph Albahari. A thread is an independent execution path with its own resources allocated by the CPU. • Threads run in parallel within a process just as. NET applications are executed, the life cycle of a thread opvibpaberland.tk, how the. . NET Security, The Complete Visual C# Programmer's Reference Guide, and.
|Language:||English, Spanish, Indonesian|
|Distribution:||Free* [*Sign up for free]|
No part of this book may be reproduced, stored in a retrieval system, or . you know that Packt offers eBook versions of every book published, with PDF and ePub . Chapter 1, Threading Basics, introduces basic operations with threads in C#. This book addresses the fundamental units of Windows and. Title Threading in C#; Author(s) Joseph Albahari; Publisher: opvibpaberland.tk (). ebook HTML and PDF ( pages); Language: English; ISBN N/A; ISBN N/A. Download PDF C# supports parallel execution of code through multithreading. A thread is an independent .. Start(); } }. Get the whole book.
Applications may have one or more processes involved, where each process has its own separation of data, execution code, and system resources. You might notice above that there is a reference to the CPU usage. This is down to the fact that each process has an execution sequence used by the computer's CPU. This execution sequence is known as a Thread. This thread is defined by the registers in use on the CPU, the stack used by the thread, and a container that keeps track of the thread's state the Thread Local Storage, TLS.
Creating a process includes starting the process running at an instruction point. This is normally known as the primary or main thread. This thread's execution sequence is largely determined by how the user code is written.
Time Slices With all these processes all wanting a slice of the CPU time cycle, how does it get managed? Well, each process is granted a slice of time quantum on which it the process may use the CPU. Multithreaded Processes What happens if we need our process to do more than one thing, like query a Web Service and write to a database at the same time?
Luckily, we can split a process to share the time slice allocated to it. This is done by spawning new threads in the current process. These extra threads are sometimes called worker threads. These worker threads share the processes memory space that is isolated from all other processes on the system. The concept of spawning new threads within the same process is called free threading. As some of you may as I did come from VB 6. Let's see some figures, shall we, as this is fairly important?
With this model, each time you want to do some background work, it happens in its own process, so is known as Out Of Process. With Free threading, we can get the CPU to execute an additional thread using the same process data.
This is much better than single threaded apartments, as we get all the added benefits of extra threads with the ability to share the same process data. Note: Only one thread actually runs on the CPU at one time. If we go back to the Task Manager and change the view to include the thread count, we can see something like: This shows that each process can clearly have more than one thread.
So how's all this scheduling and state information managed? We will consider that next. Thread Local Storage When a threads time slice has expired, it doesn't just stop and wait its turn.
Recall that a CPU can only run one thread at a time, so the current thread needs to be replaced with the next thread to get some CPU time. Before that happens, the current thread needs to store its state information to allow it to execute properly again.
This is what the TLS is all about. One of the registers stored in the TLS is the program counter, which tells the thread which instruction to execute next. Interrupts Processes don't need to know about each other to be scheduled correctly. That's really the job of the Operating System. Even OSs have a main thread, sometimes called the system thread, which schedules all other threads. It does this by using interrupts. An interrupt is a mechanism that causes the normal execution flow to branch somewhere else in the computer memory without the knowledge of the execution program.
The OS determines how much time the thread has to execute, and places an instruction in the current thread's execution sequence.
Since the interrupt is within the instruction set, it's a software interrupt, which isn't the same as a hardware interrupt. Interrupts are a feature used in all but the simplest microprocessors, to allow hardware devices to request attention. The key difference is that processes are fully isolated IDE from each other; threads share heap memory with other threads running in the same application.
This is what makes threads useful: Now with autocompletion! The main thread keeps running, while the worker thread does its background job.
With Windows Forms or WPF applications, if the main thread is tied up performing a lengthy operation, keyboard and mouse messages cannot be processed, and the application becomes unresponsive. The modal dialog approach also allows for implementing a "Cancel" button, since the modal form will continue to receive events while the actual task is performed on the worker thread.
The BackgroundWorker class assists in just this pattern of use. Having a worker thread perform the task means the instigating thread is immediately free to do other things. Such methods can execute faster on a multi-processor computer if the workload is divided amongst multiple threads. One can test for the number of processors via the Environment.
ProcessorCount property. A C application can become multi-threaded in two ways: either by explicitly creating and running additional threads, or using a feature of the. NET application. In these latter cases, one has no choice but to embrace multithreading. Asynchronous methods follow a similar protocol outwardly, but they exist to solve a much harder problem, which we describe in Chapter 23 of C 4.
BeginInvoke returns immediately to the caller. You can then perform other activities while the pooled thread is working. Second, it receives the return value as well as any ref or out parameters. Third, it throws any unhandled worker exception back to the calling thread.
In practice, this is open to debate; there are no EndInvoke police to administer punishment to noncompliers!
Optimizing the Thread Pool The thread pool starts out with one thread in its pool. You can set the upper limit of threads that the pool will create by calling ThreadPool. SetMaxThreads; the defaults are: in Framework 4. The reason there are that many is to ensure progress should some threads be blocked idling while awaiting some condition, such as a response from a remote computer.
You can also set a lower limit by calling ThreadPool. Raising the minimum thread count improves concurrency when there are blocked threads see sidebar.
The default lower limit is one thread per processor core — the minimum that allows full CPU utilization. On server environments, though such ASP. Rather, it instructs the pool manager to create up to x threads the instant they are required. To illustrate, consider a quad-core computer running a client application that enqueues 40 tasks at once.
If each task performs a 10 ms calculation, the whole thing will be over in ms, assuming the work is divided among the four cores.