Reader small image

You're reading from  Expert Delphi - Second Edition

Product typeBook
Published inFeb 2024
Reading LevelExpert
PublisherPackt
ISBN-139781805121107
Edition2nd Edition
Languages
Right arrow
Authors (2):
Marco Cantù
Marco Cantù
author image
Marco Cantù

Marco Cantù is an experienced Delphi expert, who started working with the product since its introduction in 1995. He is currently working as a Product Manager for RAD Studio at Embarcadero Technologies, an Idera company. Prior to that, Marco was a Delphi trainer and consultant for Wintech Italia. Over the years, Marco has written 20 books on Delphi, from the classic Mastering Delphi series to the recent Object Pascal Handbook. Marco has been a speaker at many Delphi and programming conferences worldwide, including over 10 Borland US Conferences, the Software Development Conference, Borland European conferences, EKON (Germany), DCon (UK), Conference to the Max (Holland), DelphiTage, the Italian Delphi Day, and a few editions of Delphi Developer Days. Marco is based in Italy.
Read more about Marco Cantù

Paweł Głowacki
Paweł Głowacki
author image
Paweł Głowacki

Paweł Głowacki was Embarcadero's European Technical Lead for Developer Tools. Previously, Paweł spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. Apart from working with Embarcadero customers across the region, he represented Embarcadero internationally as a conference and seminar speaker. Paweł passed away in mid-December 2017, but he is alive in the hearts of the Delphi developers community, worldwide.
Read more about Paweł Głowacki

View More author details
Right arrow

Using the Parallel Programming Library

Any modern CPU has a multi-core architecture. This means multiple applications can run in parallel at the same time in the foreground or the background. That’s not specifically interesting for developers. What’s relevant, though, is that a multi-core CPU allows single applications to use more than one core at the same time, spawning additional threads of execution and running them in parallel.

While this feature offers a lot of power to CPU-bound applications (applications that need to perform a lot of calculations or CPU operations), it is one of the most difficult to use as a developer. This is because multithreaded applications need to be written with a lot of care since different threads might access the same resources at the same time. Hence, threads need to synchronize any shared resource access, which is far from a simple operation in many cases.

This chapter will cover the following parts:

  • Using threads
  • ...

Technical requirements

All the code in this chapter can be found in the following link: https://github.com/PacktPublishing/Expert-Delphi_Second-edition

Using threads

When an operating system starts an app, it creates a process for it and starts a thread of execution, often called the main thread. As I mentioned before, in each process, there could be one or more threads running, which is more relevant at a time when all processors that power computers and mobile devices have multiple cores. This means that there could be multiple threads executing in parallel on each core. A typical app executes in one thread, which runs on just one processor core. All other cores do nothing.

There are, however, multiple reasons for using threads. The most important is not the extra performance, but keeping an app responsive while it is doing some heavy calculations or waiting for external information (for example, using an HTTP request). Another scenario is using multiple threads for faster calculations, which is more enticing but, as mentioned, also the most complex one.

Since the early versions of Delphi, there is a TThread class in the Runtime...

Access synchronization

When synchronizing access, as we saw in the previous demo, one of the main issues with multithreaded applications is the need to avoid multiple threads accessing the same variable or the same resource at the same time. If one thread is making a change to a data structure while another thread is reading from it, the result will be totally unpredictable. Synchronized access to resources is the most complex issue of threading.

There are a large number of techniques you can use for thread synchronization and we don’t have the scope to explore them all extensively in this chapter. I will just introduce the most common and simple options here.

Calling Synchronize

The first option is the one we have already used: the Synchronize method of the TThread class. The idea is simple: there is some code that needs to be executed in the context of the main thread. This is the case for UI-related code, as mentioned earlier. In general, if you need to make sure...

Exploring the Parallel Programming Library

On top of this core foundation, Delphi offers the more abstract Parallel Programming Library (PPL). This library allows you to express your application logic in a way that is less tied to the actual hardware and the capabilities of the CPU the program is running into. To clarify, the core element of the PPL is the TTask class; this is conceptually similar to a thread but abstracted from the hardware. For example, if you create 20 threads, you are making requests to the operating system and the CPU. If you create 20 tasks, the PPL will create some threads in a pool, depending on the CPU multicore capabilities, and assign tasks to those threads, without overloading the CPU. Also, reusing threads saves time, as thread creation is a heavy operation at the operating system level.

The very first thing to do in order to use the PPL is to add the System.Threading unit to the uses clause of your program. As mentioned, on behalf of the app, the library...

Summary

In this chapter, we have focused on one of the most important and most complex features of modern computing: multithreaded applications and parallel programming. We had room only for an overview to get you started in this area.

In particular, we delved into Delphi’s Parallel Programming Library. With constructs such as parallel for loops, tasks, and futures, we can really make our Delphi apps faster and more responsive. However, keep in mind that you need to be careful in terms of resource access and synchronization, and for this reason, we introduced some of the key concepts in this space, from critical sections to TMonitor and the intrinsic operations.

If you want to learn more about threading and parallel programming, I fully recommend reading books and articles by Primoz Gabrijelcic, particularly his recent book, Delphi High Performance by Packt (ISBN: 9781805125877).

Now that we have looked into the foundations of the Delphi language and touched on some...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Expert Delphi - Second Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805121107
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Marco Cantù

Marco Cantù is an experienced Delphi expert, who started working with the product since its introduction in 1995. He is currently working as a Product Manager for RAD Studio at Embarcadero Technologies, an Idera company. Prior to that, Marco was a Delphi trainer and consultant for Wintech Italia. Over the years, Marco has written 20 books on Delphi, from the classic Mastering Delphi series to the recent Object Pascal Handbook. Marco has been a speaker at many Delphi and programming conferences worldwide, including over 10 Borland US Conferences, the Software Development Conference, Borland European conferences, EKON (Germany), DCon (UK), Conference to the Max (Holland), DelphiTage, the Italian Delphi Day, and a few editions of Delphi Developer Days. Marco is based in Italy.
Read more about Marco Cantù

author image
Paweł Głowacki

Paweł Głowacki was Embarcadero's European Technical Lead for Developer Tools. Previously, Paweł spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. Apart from working with Embarcadero customers across the region, he represented Embarcadero internationally as a conference and seminar speaker. Paweł passed away in mid-December 2017, but he is alive in the hearts of the Delphi developers community, worldwide.
Read more about Paweł Głowacki