Skip to main content

Threading!

What is Threading?

Threads are a relatively lightweight way to implement multiple paths of execution inside of an application. At the system level, programs run side by side, with the system doling out execution time to each program based on its needs and the needs of other programs. Inside each program, however, exists one or more threads of execution, which can be used to perform different tasks simultaneously or in a nearly simultaneous manner. The system itself actually manages these threads of execution, scheduling them to run on the available cores and preemptively interrupting them as needed to allow other threads to run.
From a technical standpoint, a thread is a combination of the kernel-level and application-level data structures needed to manage the execution of code. The kernel-level structures coordinate the dispatching of events to the thread and the preemptive scheduling of the thread on one of the available cores. The application-level structures include the call stack for storing function calls and the structures the application needs to manage and manipulate the thread’s attributes and state.
In a non-concurrent application, there is only one thread of execution. That thread starts and ends with your application’s main routine and branches one-by-one to different methods or functions to implement the application’s overall behavior. By contrast, an application that supports concurrency starts with one thread and adds more as needed to create additional execution paths. Each new path has its own custom start routine that runs independently of the code in the application’s main routine. Having multiple threads in an application provides two very important potential advantages:

• Multiple threads can improve an application’s perceived responsiveness.
• Multiple threads can improve an application’s real-time performance on multicore systems.

If your application has only one thread, that one thread must do everything. It must respond to events, update your application’s windows, and perform all of the computations needed to implement your application’s behaviour. The problem with having just one thread is that it can only do one thing at a time. So what happens when one of your computations takes a long time to finish? While your code is busy computing the values it needs, your application stops responding to user events and updating its windows. If this behaviour continues long enough, a user might think your application is hung and try to forcibly quit it. If you moved your custom computations onto a separate thread, however, your application’s main thread would be free to respond to user interactions in a more timely manner.
With multicore computers common these days, threads provide a way to increase performance in some types of applications. Threads that perform different tasks can do so simultaneously on different processor cores, making it possible for an application to increase the amount of work it does in a given amount of time.
Of course, threads are not a panacea for fixing an application’s performance problems. Along with the benefits offered by threads come the potential problems. Having multiple paths of execution in an application can add a considerable amount of complexity to your code. Each thread has to coordinate its actions with other threads to prevent it from corrupting the application’s state information. Because threads in a single application share the same memory space, they have access to all of the same data structures. If two threads try to manipulate the same data structure at the same time, one thread might overwrite another’s changes in a way that corrupts the resulting data structure. Even with proper protections in place, you still have to watch out for compiler optimizations that introduce subtle (and not so subtle) bugs into your code.

Comments

Popular posts from this blog

Von Neumann machine

Von Neumann Machine is the basic architecture of the modern computer as it laid the foundation for same memory usage for program and data.

The Von Neumann Machine consists of three main components :
Main MemoryC.P.UI/O devices

Basic Linux Links and usefull stuff

Hey guys
You  might be looking for Basic Linux stuff then you are at the right place... :D
I started using Linux about 5 years ago but got into it (seriously!) 1 and a half year before,
on going through the basics which i learned from many documentation provided by THE AWESOMELY GREAT ~#TLDP#~ -->The documents it provided me : <<Download PDF if possible because it is formatted nicely in it compared to others (Not compulsory Totally Ur call) >> %  Introduction to Linux - A Hands on Guide: It provided me real one on one interaction with THE GREAT OS EVER :D It gave me total overview of Linux and awesomely cool tricks. It also contains exercise at the end of each lesson or chapter. You should give it a look (serious advice :P) % Bash Guide for Beginners: Before starting with bashing you should know bare minimum concepts of Linux <which includes placement of files, commands interaction with term and much more> which you can take from the guide above (told ya that you shoul…