Understanding threads and the threading process

A primary thread is started whenever a .NET program is started. Additional threads are created by this primary thread to execute the application login either concurrently or in parallel. These threads are called worker threads. These threads can execute any part of the program code, which may include parts that are executed by another thread. As these threads are free to cross application boundaries, .NET Framework provides a way to isolate these threads within a process using application domains (not available in .NET Core).

If our program can perform multiple operations in parallel, it will drastically decrease the total execution time. This can be achieved by utilizing multiple threads with multiprocessors or the multicore environment. The Windows operating system, when used alongside .NET Framework, ensures that these threads complete their respective tasks. Managing these tasks does have overhead, however. The OS allocates each thread a certain period of CPU time so that they can execute. After this period, a thread switch happens, which is called context switching. This context is saved and restored for each switch. To do this, Windows uses CPU registers and state data.

In an environment where multiple processors and multicore systems are available, we can take advantage of these resources and increase the throughput of the application. Consider a Windows application in which one thread (the primary thread) is handling the user interface by responding to user actions and other threads (worker threads) perform operations that require more time and processing. If the primary thread completes all of these operations, the user interfaces won't be responsive.

Because of this overhead, we need to carefully determine when to use multithreading.

In the upcoming sections, we will focus on how we can create and manage threads, understand different thread properties, how we can create and pass parameters to threads, the difference between foreground and background threads, how to destroy threads, and more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.228.78