The concept of concurrency & multithreading: What are they and how they function
May 17, 2022
As the world around us moves ever more online, the need for software engineers is growing - seemingly by the hour. Whether by building firewalls that fend off the next big data breach, or developing the next app that goes viral, we have a seemingly inexhaustible need for them at this moment in time.
For those out there already in the field, writing code on a daily basis, the concepts of multithreading and concurrency will not be new to you. To anyone looking to break into this ever-expanding industry, it’s time to take note. Not only are they going to make your life easier, they’re also a great way to differentiate yourself when it comes to applying for jobs in this space.
Their names don’t sound hugely accessible, but—as with most things in the software industry—they come with a label that has a bark that is worse than its bite. In this article, we’re going to take a look at both multithreading and concurrency, explaining what they are, why you should use them, and how they work.
What is concurrency and multithreading?
So, let’s start with the basics. There’s no point in taking a look at a concept and whether or not it’s something you want to learn more about if its fundamental aren’t firmly understood.
Simply put, multithreading is a software engineering technique that enables the engineer to support the simultaneous execution of more than one part of a program. This delivers an optimized CPU, as people can do a number of things at the same time. With programs being comprised, at their most basic level, of threads, this is where the term comes from. A real world example of this would be using one program to play music whilst having another set up for the user to write code.
Concurrency, as a term, is something that multithreading can lay the groundwork for. It is a name that refers to a program which you’re running or have built, and its ability to deal with numerous things at once. The most important word there is “deal”, as it is easy to equate concurrency with another concept known as parallelism, which actually refers to a program’s ability to do multiple things at the same time.
Why would you use concurrency and multithreading?
Now that the concepts have been outlined, let’s move on to why you would bother using them in the first place. You can understand as much theory as you like, but if you aren't able to apply them anywhere, or understand when they should/shouldn’t be used, there’s little point in knowing about them at all.
The uptick in the popularity of multithreading coincided with another shift in the industry. When using just a single core, an application, whether native or otherwise, would multitask only through illusion. It would have to transition back and forth, without actually multithreading, calling usability into question.
With the advent and uptake in the use of multiple cores, this issue became null and void. Applications built with multiple cores gained the advantage of being able to use the underlying hardware to run individual threads through a dedicated core. This makes them more responsive and efficient to run, and thus, more popular.
Aside from usability, it also means that the application itself is optimized. Taking full advantage of all that the CPU has to offer means the app realizes its full potential, and doesn’t leave some cores idle whilst others are working.
It seems, from the advantages outlined above, that there is limitless potential in apps and cloud web hosting that are multithreaded.
Well, yes and no. Whilst running multiple threads through multithreading leads to a concurrency that the user can enjoy, each new thread added brings with it the potential for problems.
This means that every new one introduced should be done out of necessity, not want. They need to be thoughtfully and thoroughly understood before entering the mix of the application and potentially destabilizing it.
Similarly, multithreading isn’t a solve-all. It works wonderfully for some apps, but for others, it just proves to be unnecessary, meaning that it is beneficial to abandon the idea altogether. It’s the equivalent of a DTLS protocol in that it is mightily effective when used for a system it was designed for.
If, for example, your app performs sequential operations, for the most part, using multithreading with the aim of achieving concurrency makes little to no sense. It leaves the application feeling as though it is all style and no substance.
Finally, there are equally intimidatingly named side effects to multithreading and concurrency, such as “livelock” and “deadlock”. These speak to issues that arise when multithreading is warranted but executed without the care and attention required to do so successfully. This may not lead to intrusion through a computer bug, but it sure slows down the process of app development in the same way.
Livelock refers to a situation where two threads take actions in response to each other rather than making progress. It is the software equivalent of getting in a battle in a motor race. Sure, you may gain a position, but battling too much only ends up slowing both drivers/riders down.
Deadlock speaks to a situation when neither thread can make any progress. This time, the issue is caused by a reliance that each thread has on the other, because both threads hold a resource required by the other in order to proceed as intended.
In this article, we’ve looked at concurrency and multithreading. We’ve outlined what both concepts refer to and how they are linked. We’ve also taken a look at how and why they should be used by any budding software engineer. Finally, we’ve highlighted some potential issues that both concepts can cause.
Whether you’re looking to get started in the software engineering game or looking to add another valuable skill to your toolkit, multithreading and concurrency offer brilliant insights into where the practice of software engineering is going. It’s going to become a skill that employers expect any professional engineer to have in the years to come, so why not take the time to get to become familiar with it now and get ahead of the competition?
About the author:
Severine Hierso is EMEA Senior Product Marketing Manager for RingCentral Office, the leader in cloud communications solutions, and is passionate about creating value, differentiation and messaging, ensuring a better experience for customers and partners. She has gained extensive international Product Marketing, Virtual PBX System, Market Research, Sales Enablement and Business development experience across SaaS, Telecommunications, Video Conferencing and Technology sectors within companies such as Sony, Cisco, Cogeco Peer 1 and Dimension Data/NTT.
Ready to take charge of your dev career?
Join Europe's leading job platform for software developers!