What Is Multithreading?
Multithreading is an important utility for many computer programs, and can help your own programs increase performance efficiency.
Your computer (or phone/tablet) runs all of its software through its CPU, which processes instructions. Often, CPUs come with multiple cores, and each core can process a certain number of threads, meaning that the CPU can handle multiple instructions at the same time.
Multithreading lets a program take advantage of this by efficiently dividing workloads over separate threads to get the work done quicker. In the next section, we'll go over an example that demonstrates how multithreading is quicker than single threading.
How To Multithread In C++
We know what multithreading is, but how do we implement it in our own programs? In C++, it's quite simple. We need to do the following:
1. Include the thread library in our program.
2. Create a thread worker.
3. Call that thread worker on the function we want to spin off on a separate thread.
Let's demonstrate this with an example.
We're going to create a function to kill time. The function, aptly named KillTime, will simply iterate through a while loop.
We're going to call this function three times using the normal way, and then three times by spinning the function off onto a separate thread each time.
You can also find the source code on github, linked here. If you'd like a video demonstration where I walk through the code with you, then please watch this youtube video.
The "NormalWay" calls KillTime, and when it ends it calls KillTime, and when that ends it calls KillTime. Each KillTime must wait for the previous KillTime to finish before executing.
The "MultithreadedWay" sends KillTime off onto a separate thread (if one is available), and the main thread continues executing the next line of code immediately, which will send another KillTime off onto a different thread, and finally the main thread will send the third KillTime off on another thread. Each KillTime will happen simultaneously, without needing to wait for another to finish.
When I ran the code on my own computer, the NormalWay code took 7.8 seconds to finish, and the KillTime took 2.6 seconds to finish. Multithreading is quicker, yay!
When To Use Multithreading
Multithreading makes our program much more efficient, and in many cases quicker. There are two special occasions that we should use multithreading:
1. When we're performing a long calculation that takes time, it may be better to divide the work into separate threads, if possible.
2. If our application has separate features that don't need to be coupled, it's better to separate work off on a separate thread. (This is always true for applications where we have a UI and need to perform a calculation asynchronously.)
Use multithreading where necessary to make programs run quicker (and get the most out of your CPU).
Multithreading is a nice feature, but it should be used with caution. There are a few fatal mistakes that are possible if multithreading is misused. In another article, we'll go over how to properly multithread.
As a sneak peak, the things a good multithreading application is aware of is:
1. Not sharing variables between threads.
2. Ensuring that threads are properly disposed of.
3. Only using locking a thread for when necessary.
To see the next article that goes over a more in-depth analysis of multithreading, click here.
If you're prefer to watch the youtube video, where I demonstrate what each line of code means, then please watch the video embedded below.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Hi, I'm srcmake. I play video games and develop software.
Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.