Most devices and computers now have multiple CPU units, even if the phone is multi-core. But the advantage of using multicore in development requires some skill and time to write extra code. OK, now you can use Delphi to do parallel programming.
In Delphi, C + + builder, and Rad Studio XE7, there is a library that simplifies parallel running tasks, called parallel programming libraries.
Parallel Programming library in the System.Threading unit, which provides a lot of useful features, can be easily applied to existing projects and new projects. Provides a number of handy overloaded functions that can support both C + + and object Pascal.
These features include easy-to-use parallel operations for loops and a number of advanced features that support operations such as running tasks, join tasks (waiting for other threads to complete), and waiting for a set of tasks. These features are supported by a self-managed thread pool (based on the number of CPUs).
To demonstrate how easy it is to use parallel libraries, we write an example of calculating prime numbers.
function IsPrime (N:integer): Boolean;
Var
Test:integer;
Begin
Result: = True;
for Test: = 2 to N-1 do
if (N mod Test) = 0 Then
begin
Result: = False;
Break {Jump out of the For loop}
End;
End;
The typical algorithm for obtaining a mass number from 1 to x is a sequential loop that examines each of these numbers and records the total in a variable (here is an integer tot).
Const
Max = 50000; 50K
for I: = 1 to Max do
begin
if IsPrime (I) Then
INC (Tot);
End;
Using the new parallel library, you can replace the for loop with a class function tparallel.for and pass an anonymous method.
In addition, to avoid multi-threaded collisions, you can call tinterlocked.increment.
Tparallel.for (1, Max, procedure (I:integer)
begin
if IsPrime (I) Then
Tinterlocked.increment (Tot);
end);
What are the improvements?
Using the Tstopwatch class of the System.Diagnostics unit makes it easy to get the run time of each cycle. Even when running in a 2-core virtual machine, the standard loop takes 415ms, while parallelism requires only 192ms. Time on Mac dropped from 382ms to 90ms.
The most popular place is that, as part of the language and framework, it can be easily added to existing code.
The advantages of multi-core devices, including mobile devices, can be exploited when developing native code. However, running too many threads on a mobile device consumes more power.
Samples
Examples of other parallel programming libraries are the examples of Conways games that Delphi and C + + comes with:
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object Pascal\rtl\parallel Library
C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\CPP\RTL\Parallel Library
I do not know how you feel, I have used parallel library to speed up my old program, programming happy.
http://blog.csdn.net/henreash/article/details/41315183
http://blog.csdn.net/henreash/article/details/41349145
http://blog.csdn.net/henreash/article/details/41347843
Delphi Xe7 Parallel Programming Quick Start (three articles)