Tuesday, January 1, 2013

What is threading

What is a thread:
  • A thread is the entity with in a process that can be scheduled for execution.
  • Threads are much like lightweight processes.
  • Allow the program to execute sequential actions or many actions at once.
  • All the threads in a process share the same address space.
  • It's a small independent chunk of code that can run concurrently with other threads.
  • Each thread runs independently of the others.
  • Each thread may run a different sequence of instructions.
  • A thread isn't a whole program.
  • Each process has atleast one thread.
Note: It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process.

What is a process:
  • An executing instance of a program is called a process.
  • Program under execution is known as process.
  • A process is always stored in the main memory also termed as the primary memory or random access memory.
  • Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
  • Each process provides the resources needed to execute a program.
  • Several process may be associated with a same program.
  • A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution.
  • Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

What is the difference between Process and Thread :
  1. Threads are easier to create than processes since they don't require a separate address space.  
  2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.
  3. Threads are considered lightweight because they use far less resources than processes.
  4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other. This is really another way of stating 2 above.
  5. A process can consist of multiple threads.
  6. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  7. Both threads and processes are atomic units of OS resource allocation.

What is Threading (or multithreading):
  • Multithreading is the ability of a program or process to manage multiple requesets by the same user at same time without having to have multiple copies of the program (that is running).
  • If a program have multiple thread that kind of program known as multithreading program.
  • Threads allows for multithreading.

Advantage of threading:
There are 2 main reasons to use threading in an application.
  • Multithreading improve application performance.
  • Separation of concerns.

When not to use multithreading:
  • The only reason not to use threading is when the benefit is not worth the cost.
  • The performance gain might not be as large as expected.

Disadvantage of threading:
  • It can complicate the code, making it harder to understand and more prone to bugs. Therefore it’s only worth doing for those performance-critical parts of the application where there’s the potential for measurable gain.

Why we use Threading:
  • In simple, to achieve concurrency.

What is concurrency:
  • Concurrency is about two or more separate activities happening at the same time.
  • In term of computers, a single system performing multiple independent activities in parallel, rather than sequentially, or one after the other.
  • Concurrency meaning in plain English -->Acting together, as agents, circumstances or events.

Why use concurrency:
The almost same reason that for threading.
  • Separation of concerns.
  • Performance.
           
Explaination for separation of concerns:
Separation of concerns is almost always a good idea when writing software; by grouping related bits of code together and keeping unrelated bits of code apart, you can make your programs easier to understand and test, and thus less likely to contain bugs. You can use concurrency to separate distinct areas of functionality, even when the operations in these distinct areas need to happen at the same time; without the explicit use of concurrency you either have to write a task-switching framework or actively make calls to unrelated areas of code during an operation.
Example:
A DVD player application for a desktop computer. Such an application fundamentally has two sets of responsibilities: not only does it have to read the data from the disk, decode the images and sound, and send them to the graphics and sound hardware in a timely fashion so the DVD plays without glitches, but it must also take input from the user, such as when the user clicks Pause or Return To Menu, or even Quit. In a single thread, the application has to check for user input at regular intervals during the playback, thus conflating the DVD playback code with the user interface code.

c++11 standard have the support of multithreaded programs.
c++11 standard provide components in the library for writing multithreaded application and it is known as 'thread'.
we achieve threading before c++11 standard.
what's the difference made by thread library in c++11 standard. (or why use thread library)
Well answer is: This will make it possible to write multithreaded c++ programs without relying on plateform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior.

What is Mutexes: (mighty)
  • Mutex stand for mutual exclusion and is the simple and most useful synchronization structure.
  • In essence it is a boolean that determines if an object is locked from use or not.

What is Semaphores: (super)
  • Semaphores are objects that allow a certain number of threads to access a certain object before it begins blocking it.
  • A semaphore with a value 2 will allow the first 2 threads in the queue access to it then block any threads after that.
  • A mutex is in essence just a semaphore that allows one thread at a time to access an object.

No comments:

Post a Comment