Concurrency and Multi-Threading
Concurrency. The word means “things that happen or exist together at the same time”. Programmers and engineers in the 1960s came together at IBM to officiate SMT, or Simultaneous Multi-Threading. What does this mean? This means that the guys who made a particular language have given you mortals the ability to divide a chunk of code and distribute it amongst your thirsty CPUs, mostly EVENLY!
The need arose when a LOT of code, I'm talking about code that landed us on the moon (allegedly), needed to be processed faster. When technology got lazy, smart people called programmers came up with a solution. They said that no matter how big the processing unit gets, tasks will still run slowly, unless, the code does not distribute itself everywhere on the CPU, where it does its work and then like threads that split from a single rope, come back and join.
The threads come together when we invoke a certain function and this leads to a task finally coming together.
This was the theory. What follows is the code I have written in C++ with a few abstract concepts such as lambda functions and Object Oriented Programing. So continue at your own risk.
(1) Imports
This is all you need to import for the simple example I show. I would recommend you continue typing it out as you read through this.
(2) The Callable
The callable object can return the computed result done by a thread in contrast to a runnable interface which can only run the thread.
In my example, I will use three different types of callable objects and the available approaches absolute beginners.
The first one is a simple function as a callable.
The second one will be an entire class, who’s callable object will be created because C++ is object oriented so why not.
And the final one is our beloved lambda function. If you have no idea what it is or aren't confident enough then sure skip this part.
I hope you're following up in your fancy IDEs and for the OGs, your VIMs hehe.
The part that comes next is where the magic happens. The main() function.
The first three lines of code are the creation of threads (th1, th2 and th3). These three lines use that #include <thread> import and internally divide the code into three separate independent procedures which will take their places in the CPUs.
Assume you have a quad-core system (4 core/CPU system). Then these threads will take randomly each choose a CPU and continue their work there. This is what dividing a procedure with two threads would look like inside :
The single main thread goes through the library <thread> and divides itself according to how many threads we tell the compiler to create. The line of code that does that is this one :
The threads will then combine using the “.join()” function.
That's it I guess, at least for beginners like me. Concurrency includes many more properties, such as asynchronous programming, futures and promises, attributes, load factoring and much more. Those are not really something I can comment on just yet because its taking me time to condense it for myself.
At this point, try to incorporate all of this into a basic program which maybe, prints primes from 0 to a given range. It wont make any difference if you used a single thread to do it or multiple, because finding primes is not heavy task.