Now, if you’ve ever used Make before, you know how frustrating it can be to wait for all those ***** tasks to finish running on just one thread. But don’t freak out! With the magic of job servers and multi-threading, your build times will be cut in half (or more)!
So Let’s kick this off with this amazing feature that’ll make you feel like a coding superhero. First things first what exactly is a job server? Well, it’s basically a fancy way of saying “a system for managing multiple tasks at once.” In other words, instead of running each task one by one in sequence (which can be slow and boring), Make will automatically distribute the workload across all available CPU threads.
And that’s not all! With autodetection of CPU threads, you don’t even have to worry about setting up your own job server Make will do it for you! All you need to do is add a few simple lines of code to your makefile (which we’ll cover in just a sec), and voila instant multi-threading goodness.
So how does this work exactly? Well, when you run Make with the “-j” flag (which stands for “job server”), it will automatically detect the number of available CPU threads on your machine and distribute tasks accordingly. For example, if you have a quad-core processor, Make will use all four cores to run tasks simultaneously which means faster build times!
But that’s not all! With autodetection of CPU threads, Make can also handle dynamic changes in the number of available resources. For example, if you add more CPUs or switch to a different machine with fewer cores, Make will automatically adjust its behavior accordingly which means even faster build times (or slower ones, depending on your situation).
So how do we set this up in our makefile? Well, it’s actually pretty simple! All you need to do is add the following lines at the beginning of your makefile:
make
# Set number of jobs (threads) for Make
# This line adds the MAKEFLAGS variable to the makefile, which allows us to set additional flags for Make to use during the build process.
MAKEFLAGS += -j $(shell getconf _NPROCESSORS_ONLINE)
# This line sets the number of jobs (threads) for Make to use, based on the number of processors available on the current system.
# The -j flag tells Make to run multiple jobs in parallel, which can significantly speed up the build process.
# The $(shell) function executes the command within it and returns the output, in this case the number of processors available on the system.
# The getconf command is used to retrieve system configuration information, and the _NPROCESSORS_ONLINE option specifically returns the number of processors currently online.
# By setting the number of jobs to the number of processors, we can maximize the use of available resources and potentially reduce build times.
This will automatically set the “MAKEFLAGS” variable to include the “-j” flag, which tells Make to use all available CPU threads. And that’s it! Now you can run your build with lightning-fast speed and enjoy the sweet taste of victory (or defeat, depending on how well your code is written).
With this powerful feature at your fingertips, you can say goodbye to slow build times and hello to coding superhero status!