What is multithreading?

Multithreading is a programming and execution model that allows many processes to happen at the same time. This requires a multi thread processor however pretty much every modern processor is divided into cores. They are physically isolated parts of processor and every core can be further divided into logical cores (threads). These threads share the processors resources in a way that increases the responsiveness of the app. If there is a long-term task in main application thread, it can cause the app to freeze and stop working and in worst case OS will kill your app. Multithreading allows the app to still work properly while executing the task. This is the way to create concurrent computing in your device. It works as if there are multiple processors within the single processor core.

How Is Multithreading Being Developed?

Back in the day where ware only single-core processors. After many years on technological progress, there were multi-core processors developed. The most popular technology that allows implementing multithreading to the cores of multi-core processor is Hyper Threading made by Intel. Operating system sees it as if there is a processor with a number of virtual cores. When writing the program developers create threads using pthread library or some higher level thread api library (for example QThread class from QT Framework library, that inherits from pthread library). Furthermore you can pass a set of instructions to perform in those threads. Threads created like that are being managed by kernel of operating system. Thread scheduler takes care of maintaining the right order of working tasks.

How To Speed Up Your Device Using Multithreading

Signs That You Need To Use Multithreading

Your app/device might be working slowly during some tasks. If that’s the case you should check the load of the processor. You can use a system-monitor process-viewer (for instance htop). Bellow you can see how the processor is divided into separate threads. The first one is on 100% while the rest is almost unused. If that’s how your processor looks like, it’s time to think about rebuilding your app. However, it doesn’t have to be necessarily on 100%. It’s enough if one of them uses significantly more space that the others for a long period of time during normal work of the app. This can be even the reason your app shuts down or frizzes. Your app might be already written using multithreads. However, there might be a situation where the tasks are placed badly. This is also a reason to consider updating your software.

Multithreading - speed up your application

Properly written software using multithreads should give you a result like this one:

Multithreading - speed up your application

 

Using multithreading is indicated when there’s a need for an app to perform heavy operations or process a lot of data while maintaining stable flow of the app interface. Your app might have been programmed to work with fewer data. The issue also might have arise due to the growth and expansion of your business. In that case, it’s important to update the app.

Risks of bad multithreading

  • Deadlocks – It’s a situation when two tasks are waiting for each other to finish because they need to use the same resources as the other task. To prevent this we use mutexs or communication pipelines between threads.
  • Starvation – A task can never access the resources it needs to be done because some other task uses these resources constantly.
  • Race conditions – This error occurs when both threads are trying to access and change the data at the same time. It often happens when thread A works in a ‘check-then-act’ way. If thread B changes the value of the data in the meantime, thread A can’t do the task.
  • Livelock – It’s a situation when both tasks want to get an access to the same resource BUT they both want to give the other thread the access to this resource as well. This happens when both tasks can communicate with each other and are open to give access to the same resource at any time. However, they immediately attempt to regain the access. It’s as if two people want to let the other person through the door first. They both end up trying to enter so no one eventually does.

How We Used Multithreading To Optimize The Performance Of The Device

  • Case

A good example of using multithreading as a solution to slowly working device is our project for video surveillance recorder. Our client wanted to develop a new, better version of  a video recorder for public transport. Previous version needed to be updated. It also had a limit of 16 cameras that were able to be connected to the recorder.

  • Challenge

Our client needed the device to work 1. faster and 2. on more cameras connected. The whole device was already operating in multi threads. However they needed to be optimize. The main thread also required lightening. This was the reason why the device was so slow and unable to work properly. The challenge was that the device itself and its hardware were not much better than the last version.

  • Process

The device wasn’t able to operate properly with more than 16 cameras. Because the buffering of the footage was taking the whole space, the whole stream would shut down. The ‘out of memory’ error would show up and froze the whole app. Our developers used multithreading as a way to optimize the usage of processor. They used  4 threads to optimize the device. The biggest tasks like copying the footage and data synchronization are now happening in separate threads. Lighter processes were placed in the same thread without affecting the efficiency of the device.

  • Result

Using 4 threads allowed them to lighten the processor enough to be able to manage not 16 but 36 cameras at the same time. Moreover, this also speed up the app overall and stopped processor from overloading. Currently the maximum load of all the threads is less than 50% while executing the biggest tasks.

Summary

Multithreading can be a solution to the efficiency issue of your device or an app. It’s important to remember that writing a software in multiple threads requires a multi threaded processor. It’s impossible to perform on a one-thread processor.

Custom Software Development Company - Web Development | C++ Development | IoT Development - Get An Estimate Of Your Project

We also recommend reading: 

Related Posts