Writing your first threaded program
In this first recipe, we'll write a small program that checks whether two numbers are prime numbers—in parallel. While those two numbers are checked, each in their own thread, another thread will write dots in the terminal to indicate that the program is still running. A total of three threads will run in this program. Each thread will print its own result, so there's no need to save and return the values in this program.
Knowing the basics of threading will give the foundation to move along to more advanced programs.
Getting ready
For this recipe, you'll need the htop
program so you can see the CPU load go up for two CPU cores. Of course, other similar programs work as well, such as KSysGuard for K Desktop Environment (KDE). It's also best if your computer has more than one CPU core. Most computers today have more than one core, even Raspberry Pis and similar small computers, so this shouldn't be a problem...