- By Rod Stephens
A process is an instance of a program running on a computer. (Note that you could have multiple instances of the same program running. For example, you might have two browsers open or two instances of WordPad running.) A thread is a sequence of instructions within a single process that may execute in parallel with other threads. Sometimes you can execute multiple threads within the same process at the same time. Each thread keeps track of its position within the program’s code and can move through the code as it needs to without interfering with the other threads. This is called multithreading.
For example, suppose you write a program that takes a stock’s historical prices, performs some sort of complex statistical calculation, and predicts the stock’s future price. (If you can get that last part to work reliably, let me know!) Now suppose you want to perform the same task for several stocks. You could have the program perform the calculations sequentially, one after another. If each calculation takes about 30 seconds and you want to predict prices for 10 stocks, the total time will be around 300 seconds, or 5 minutes.
Another approach would be to start 10 threads, one for each stock. A thread would perform the statistical calculation for its stock and display the result.
A single-CPU system will multitask, switching quickly back and forth between the threads to give the illusion that they are all executing at the same time. There is still only one CPU, however, so the total time will still be around 5 minutes. In fact, there is a little bit of overhead in switching between threads so the total run time may be slightly longer.
In contrast, a computer with multiple cores may truly be able to execute more than one thread at a time. In that case, the total time will be roughly the original total time of 5 minutes divided by the number of cores, plus some overhead for setting up the threads and keeping track of what they are all doing. A two-core system might require about 2.5 minutes, whereas a four-core system might need only around 1.25 minutes to finish the calculations.
Unfortunately this speed improvement isn’t automatic or free. In addition to a small (but significant) amount of overhead to set up threads, a program may pay a large performance penalty if the threads interfere with each other. Interference can take the form of several different potential problems with parallelism.