Improving your C# Skills
上QQ阅读APP看书,第一时间看更新

Threads in .NET Core

Every application in .NET starts with a single thread, which is the main thread. A thread is the basic unit that the operating system uses to allocate processor time. Each thread has a priority, exception handlers, and a data structure saved in its own thread context. If the exception is thrown, it is thrown inside the context of the thread and other threads are not affected by it. The thread context contains some low-level information about, for example, the CPU registers, the address space of the thread's host process, and so on.

If an application is running multiple threads on a single processor, each thread will be assigned a period of processor time and will be executed one after the other. The time slice is usually small, which makes it seem as if the threads are being executed at the same time. Once the allocated time is over, the processor moves to the other thread and the previous thread wait for the processor to become available again and execute it based on the time slice allocated. On the other hand, if the threads are running on multiple CPUs, then they may execute at the same time, but if there are other processes and threads running, the time slice will be allocated and executed accordingly.