Right Money Management App For Your Financial Goals
Deep Dive into Futures and the Parallel Programming Library
1. EMBARCADERO
TECHNOLOGIESEMBARCADERO
TECHNOLOGIES
Deep
Dive
into
Futures
and
the
PPL
Jim
McKeeth
Lead
World
Wide
Developer
Evangelist
/
Engineer
jim.mckeeth@embarcadero.com
@JimMcKeeth
Tuesday,
the
10rd
February,
2015
2. EMBARCADERO
TECHNOLOGIES
This
Skill
Sprint
Works
with
.
.
.
Windows
Mac
OS
X
Android
iOS
RAD
Studio
Appmethod
Object
Pascal
C++
• RAD
Studio
XE7
DocWiki
– http://embt.co/latestdocwiki
http://docwiki.embarcadero.com/RADStudio/en/
• Appmethod
September
2014
DocWiki
– http://embt.co/AppmethodTopics
http://docwiki.appmethod.com/appmethod/topics/en/
• Appmethod
supports
the
FireMonkey
framework
on
all
4
platforms
while
RAD
Studio,
Delphi
and
C++Builder
also
support
VCL
on
Windows.
Contact
sales
with
any
questions!OP
C++
3. EMBARCADERO
TECHNOLOGIES
The
Parallel
Programming
Library
(PPL)
• Part
of
the
Run-‐Time
Library
(RTL)
in
System.Threading
• Self-‐tuning
thread
pool
(based
on
CPU
and
Load)
• Support
for:
• Parallel
For
Loops
• Tasks
-‐
Units
of
work
• Custom
Thread
Pools
• Futures
-‐
Async
values
(Last
week)
Part
1
Part
2
Part
3
4. EMBARCADERO
TECHNOLOGIES
Last
Week’s
Skill
Sprint
• Parallel
For
Loops
• TInterlocked.Increment
• TThread.Queue
• TThread.Synchronize
• For
more
information:
http://delphi.org/?p=1886
4
6. EMBARCADERO
TECHNOLOGIES
The
TTask
Class
for
Asynchronous
Work
• A
Task
is
a
unit
of
work
to
be
done
• Not
specific
to
an
individual
thread
• Implements
ITask
with
Start,
Wait,
Cancel
and
Status
• Statuses:
Created,
WaitingToRun,
Running,
Completed,
WaitingForChildren,
Canceled,
Exception
• Provides
WaitForAll
and
WaitForAny
methods
7. EMBARCADERO
TECHNOLOGIES
WaitForAll
Task
Syntax
var
tasks:
array
of
ITask;
begin
SetLength
(tasks,
2);
tasks[0]
:=
TTask.Create
(procedure
()
begin
{Long
running
work}
end);
tasks[1]
:=
TTask.Create
(procedure
()
begin
{
Other
long
running
work
}
end);
tasks[0].Start;
tasks[1].Start;
TTask.WaitForAll(tasks);
//
Blocks
until
completion
end;
OP
1
2
3
4
5
6
7
9
10. EMBARCADERO
TECHNOLOGIES
Construct
a
New
Thread
Pool
• Construct
new
TTheadPool
• Customize
Min
&
Max
worker
threads
• Pass
as
last
optional
parameter
to
Parallel
For,
Task
or
Future
• Generally
better
off
using
default
thread
pool10
var
FPool
:
TThreadPool;
//…
if
FPool
=
nil
then
begin
FPool
:=
TThreadPool.Create;
FPool.SetMaxWorkerThreads(10);
end;
//…
Use
FPool
11. EMBARCADERO
TECHNOLOGIES
Customizing
the
Thread
Pool
• Customizing
the
default
is
discouraged:
TThreadPool.Default
• SetMaxWorkerThreads(Value:
Integer):
Boolean;
• Returns
false
if
attempting
to
set
a
value
smaller
than
the
number
of
processors.
Setting
this
value
too
high
can
lead
problems
with
other
libraries
and
processes.
If
too
many
threads
execute
at
the
same
time,
task/thread
switching
overhead
can
become
restrictive.
• SetMinWorkerThreads(Value:
Integer):
Boolean;
• Returns
false
if
attempting
to
set
a
value
<
0
or
>
MaxWorkerThreads.
The
actual
number
of
pool
threads
could
be
less
than
this
value
depending
on
actual
demand.
Setting
this
to
too
few
threads
could
be
less
than
optimal
resource
utilization.
• property
MaxWorkerThreads:
Integer
• property
MinWorkerThreads:
Integer
13. EMBARCADERO
TECHNOLOGIES
Futures
-‐
Calculating
Values
Asynchronously
• Future:
Essentially
a
Task
that
returns
a
result
value
• A
future
is
to
a
task
as
a
function
is
to
a
procedure
• Future
is
defined
with
Generic
Type
of
the
result
• Calculates
result
in
the
background
asynchronously
• Will
block
on
read
if
not
finished
• Special
instructions
to
enable
for
C++
(see
download)
14. EMBARCADERO
TECHNOLOGIES
Future
Syntax
var
FutureString:
IFuture<string>;
//
.
.
.
FutureString
:=
TTask.Future<string>(
function:string
begin
Result
:=
DoRESTCall(editArtist.Text);
end);
//
.
.
.
ResultsMemo.Text
:=
FutureString.Value;
OP
1
2
3
4
5
17. EMBARCADERO
TECHNOLOGIES
Review:
Futures
and
the
PPL
• Uses
System.Threading
unit
• Self-‐tuning
thread
pool
(based
on
CPU
and
Load)
• Tasks
are
asynchronous
units
of
work
• Futures
allow
asynchronous
value
calculation
• Customizable
Thread
Pools
18. EMBARCADERO
TECHNOLOGIES
PPL
Resources
• Samples
– C:UsersPublicDocumentsEmbarcaderoStudio15.0SamplesObject
PascalRTLParallel
Library
– C:UsersPublicDocumentsEmbarcaderoStudio15.0SamplesCPPRTLParallel
Library
• DocWiki
Overview
– http://docwiki.embarcadero.com/RADStudio/en/Using_the_Parallel_Programming_Library
• Blog
Posts
– CodeRage
9
Video
http://dannywind.nl/delphi/coderage9/
– Stephen
Ball’s
series
http://delphiaball.co.uk/tag/parallel-‐programming/
– Malcolm
Groves’
series
http://www.malcolmgroves.com/blog/?cat=109
– Examples,
links
and
more:
http://delphi.org/?p=1914
(Including
the
C++
Lambda
links)
Special
offers:
http://
embarcadero.com/radoffer/
19. EMBARCADERO
TECHNOLOGIES
Next
Time….
• FireDAC:
Array
DML
• Submit
a
single
DBMS
command
with
an
array
of
parameters
• Each
command
parameter
has
an
array
of
values
• All
parameters
have
arrays
of
the
same
length
• Thursday
the
12th
of
February
– 6AM
San
Francisco
/
9AM
New
York
/
2PM
London
/
3PM
Milan
– 11AM
San
Francisco
/
2PM
New
York
/
7PM
London
/
8PM
Milan
– 5PM
San
Francisco
/
Fri
9AM
Tokyo
/
Fri
10AM
Sydney
Sign-‐up:
http://www.embarcadero.com/landing-‐pages/skill-‐sprints
2nd
Reg
Form
20. EMBARCADERO
TECHNOLOGIES
• Separating
App
Logic
from
the
UI
• Promotes
Healthy
Apps
• How
to
use
Common
Patterns
• Tuesday
the
17th
of
February
– 6AM
San
Francisco
/
9AM
New
York
/
2PM
London
/
3PM
Milan
– 11AM
San
Francisco
/
2PM
New
York
/
7PM
London
/
8PM
Milan
– 5PM
San
Francisco
/
Wed
9AM
Tokyo
/
Wed
10AM
Sydney
Next
Time….
Special
offers:
http://embarcadero.com/radoffer/
21. EMBARCADERO
TECHNOLOGIES
RAD
Studio
XE7
Special
Offers
More
details
http://www.embarcadero.com/radoffer
Save
45%
when
upgrading
from
any
previous
version
When
you
purchase
the
upgrade
with
1
year
Support
and
Maintenance!
Build
a
Mobile
Platform
for
the
Enterprise
With
Enterprise
edition
or
higher.
Powerful
Tools
to
Boost
Your
Coding
(Worth
Over
$700)
• New
Object
Pascal
Handbook
by
Marco
Cantu
• Castalia
for
Delphi
2014.11
• VCL
and
FireMonkey
Premium
Styles
• Mida
Converter
Basic