In terms of kernel scheduling, a process is a heavy unit,
whereas a thread is the smallest and lightest unit. Each process reserves its
own memory space (i.e. address space) as well as resources. Thread does not
specifically own resources except registers and stack. Each thread has to be a
part of at least one process. All threads which are part of single process
share the same address space. Overall it means that if we divide a heavy process
into multiple threads to execute concurrently (so called multithreading), we
can get the result much faster. Thinking like this feels good, but the major
responsibility on programmers, which must be known, understood, and applied in
multithreaded programs is, thread synchronization, thread communication
methodology, safe resource sharing among threads etc. In the following article
you will see about these one by one with lots of Visual Basic sample code (with
output details) written for better understanding. I have used Visual Basic 2005
for the code examples in this article.
Important Terms to Know About the Threading Model
·
Thread- The smallest unit of program or
part of a process, running in the execution area. Several threads can be the
part of a single process.
·
Process- An instance of a program. Any
single process contains at least one thread.
·
Context switching- A methodology used by
the operating system to switch the processor among threads to perform parallel
execution by giving time slices to each of them. Context
switching between processes is generally slower than context switching between
threads.
·
Kernel- Core of operating system, which
performs thread instantiation and execution. It provides several system call
interface for programmers to deal with threads to customize the execution
approach.
·
Multitasking (with multi-core CPU)- A
method of operating system in which it shares CPU time among multiple tasks
(processes) by switching between them on the basis of some predefined process
scheduling algorithms. As we all know, in single CPU machine, in truth, no two
processes can run at the same point of time. Multitasking gives a impersonate
way to it. But now multi-core CPUs are available in the
market by which more than one process can run concurrently depending upon the
number of CPU cores. Even single process is divided into sub processes
and processed by different CPU cores. In this way multi-core CPUs give better
performance.
·
Multithreading- An execution model which
allows several threads to execute independently under the context of a single
process. Multiple threads can run simultaneously, so called concurrent
execution. Threads can share process resources. So it gives faster execution
model. In other words we can say that multithreading is "multitasking
applied on processes."