Introduction Thread
Threads improve performance. They move computations to a separate logical processor. They are handled in the .NET Framework with classes from the base class library. Multithreading, even with these classes, is difficult.Just as we can make our programs modular by organizing models in terms of objects with separate local state, it is often appropriate to divide computational models into parts that evolve separately and concurrently.
1. What is Threading ?
C# Thread:. . Net Framework has thread-associated classes in System.Threading namespace. The following steps demonstrate how to create a thread in C#.
2 When to use thread pool in C#?
f you have lots of logical tasks that require constant processing and
you want that to be done in parallel use the pool+scheduler.
If you need to make your IO related tasks concurrently such as
downloading stuff from remote servers or disk access, but need to do
this say once every few minutes, then make your own threads and kill
them once you're finished.Edit: About some considerations, I use thread pools for database access, physics/simulation, AI(games), and for scripted tasks ran on virtual machines that process lots of user defined tasks.
Normally a pool consists of 2 threads per processor (so likely 4 nowadays), however you can set up the amount of threads you want, if you know how many you need.
Edit: The reason to make your own threads is because of context changes, (thats when threads need to swap in and out of the process, along with their memory). Having useless context changes, say when you aren't using your threads, just leaving them sit around as one might say, can easily half the performance of your program (say you have 3 sleeping threads and 2 active threads). Thus if those downloading threads are just waiting they're eating up tons of CPU and cooling down the cache for your real application
3. Types of threads in C#
BackgroundWorker
A good way to implement simple threading in Windows Forms programs is to use the BackgroundWorker class. This results in error-free and reliable threading. The class lacks some features you can implement by directly using threads.BackgroundWorker
ThreadPool
With the ThreadPool, you can execute many different threads in parallel and have the system recycle them as soon as possible. This is an ideal way to start many short-lived jobs. The ThreadPool type provides built-in functionality.ThreadPool ThreadPool.SetMinThreads
Lock
We show how to use the lock statement in the C# language. It also relates the Theory of Relativity to concurrent programs. The lock statement is a simplified syntax for Monitor method calls.Lock
Volatile:
The volatile modifier ensures a field is used in a thread-safe way.
This modifier can eliminate the need for lock statements.
VolatileThreadStart
You can start threads by passing an instance of the ThreadStart type in the C# language. We provide examples for starting threads in this way—with and without parameters to the thread.ThreadStart ParameterizedThreadStart
Join method. When you join a thread, the current thread stops and waits for the target thread to exit. You can use Join to ensure all threads are completed at a certain point in your program.
Join
Interlocked
The Interlocked type provides a way to change or compare a value in an atomic way. This means the read and write operations are a single step and nothing can interrupt them during normal operation of the program.Interlocked
Mutex
Continuing on, the .NET Framework provides the Mutex type in the System.Threading namespace. This type is used to synchronize processes and threads within a single process. We use Mutex to create a single-instance program.
Mutex
Sleep
By using the Sleep method, you can pause a thread and not have it incur any actual CPU usage. Instead, the thread is simply delayed for a certain amount of time, measured in milliseconds.Thread.Sleep
SpinWait:
With Thread.SpinWait, your computer can wait too, doing computationally intensive loops.
This is a long-running loop.
Caution:
Please don't expect me to pay your electricity bill if you use SpinWait.
The point of SpinWait appears to be to waste electricity.
SpinWaitAsync, await
The .NET Framework 4.5 added the async and await keywords. Async modifies a method. It indicates the method can be run on a separate thread. The await keyword waits for an async method to return. It is used within an async method.
Async, Await
Tip:
Async and await make some threading patterns easier to implement.
But they require planning and a careful design.
Process
A process is separate at the level of the operating system. It can contain multiple threads. We can use separate processes instead of separate threads. This can move some of the complexity to the operating system.Process
No comments:
Post a Comment