Wednesday, April 10, 2019

Why Multithreading

Why Multithreading? My previous post was more about what is multithreading and we contrasted that with what is multitasking.

Most of the hardware in use today be it processors in servers, laptops or those in mobile phones are all equipped with multicore processors. It is only natural to expect then that software written to run on these devices make full use of the hardware and it capabilities. Shouldn’t the performance gains be double if the same tasks which was earlier running on a single core processor is now running on a dual core processor? Actually in practice this is not the case. Extracting good performance out of a dual core processor needs programmer intervention. It is not something that happens automagically. This is explored clearly in this article in Dr. Dobbs journal “The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software” by Herb Sutter that appeared in Mar 2005. I have provided a link to the republished article.

Theoretically if a well written multithreaded application were running on a dual core processor, it would run in half the time it took to run on a single core processor. But then it is not easy to write multithreaded programs. In practice not all code will achieve the theoretical max performance. The expectation is to write well behaved multithreaded programs demonstrating that the relative performance increases remains the same when moving from dual core to quad core to octa core processors.

From an end user’s perspective, what are the benefits of multithreading? Primarily two benefits are immediately relevant. Resource sharing: All I/O Devices connected to the system, Memory and Network are all resources and threads share all these resources within the application. From end user’s perspective resource sharing is a tangible experience as they will be able to work with more than one file, or download and connect to more than one network resource (Tabbed browsing). Responsive: Not just the UI but the overall application can be made responsive and the end users once again will be able to feel the difference. Event thread in GUI is separate from the main execution thread therefore UI would be responsive even if the main execution thread is doing tasks.

From an application developer’s perspective the immediate benefit is that all the capability of the microprocessor does not sit idle. Properly done multithreading engages and utilizes all the processor cores. In a typical desktop scenario one might find that majority of the tasks are bound to performing some I/O or bound to the network or waiting on response from another application. These cause latencies when the processor is idle. The idle cycles will be utilized by other processors as the OS orchestrates and schedules different threads for execution based on some priority.

The consequence of the rise of multicore environments and to realize the potential performance gains offered by such multicore environments, programmers will have to come to terms with multithreaded programming. The need is more now than ever before. Language SDKs and Operating systems offer the framework and tools necessary to build threaded programs, however the programmer needs to have a clear understanding program behavior when there are multiple threads of execution.

In the next few posts I plan to visit concurrent programming using Java. 

No comments:

Post a Comment