Sunday, September 24, 2006

.NET : Threading

Why and when to use multithreading?

@ For most developers, the primary motivation for multithreading is the ability to perform long-running tasks in the background, while still providing the user with an interactive interface.
Another common scenario is when building server-side code that can perform multiple long-running tasks at the same time, thus, each task can be run on a separate thread, allowing all the tasks to run in parallel.

@ If we regard computer programs as being either application software or service software:
Application software uses multithreads primarily to deliver a better user experience.
Service software uses multithreads to keep CPU busy servicing and deliver scalability.

@ For interactive applications, multithreading is not a way to improve performance, but rather is a way to improve then end user experience by providing the illusion that the computer is executing more code simultaneously.
In the case of server-side code, multihtreading enables higher scalability by allowing Windows to better utilize the CPU along with other subsystems such as IO.

What the fuck is thread?

@ The term thread is short for thread of execution.
When your program is running, the CPU is actually running a sequence of processsor instructions, one after another. You can think of these instructions, one after another, as forming a thread that is being executed by the CPU.

What is process?

@ Each of the programs your computer keeps in memory runs in a single process. A process is an isolated region of memory that contains a program's code and data. The process is started when the program starts and exists for as long as the program is running.

@ When a process is started, Windows sets up an isolated memory area for the program and loads the program's code into that area of memory. It then starts up the main thread for the process. The mean thread might execute instructions taht create more threads within the same process.