Date post: | 15-Jul-2015 |
Category: |
Software |
Upload: | jim-mckeeth |
View: | 7,168 times |
Download: | 0 times |
EMBARCADERO TECHNOLOGIESEMBARCADERO TECHNOLOGIES
Deep Dive into Futures and the PPLJim McKeeth Lead World Wide Developer Evangelist / Engineer [email protected] @JimMcKeeth Tuesday, the 10rd February, 2015
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++
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
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
EMBARCADERO TECHNOLOGIES
5 Tasks - Asynchronous Units of Work
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
EMBARCADERO TECHNOLOGIES
WaitForAll Task Syntaxvar 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;
OP1
2
3
4 5
6 7
9
EMBARCADERO TECHNOLOGIES
WaitForAll Task Syntax{ _di_ITask tasks[2]; tasks[0] = TTask::Create(_di_TProc(new TCppTask(3000, Label1))); tasks[1] = TTask::Create(_di_TProc(new TCppTask(5000, Label1)));
tasks[0]-‐>Start(); tasks[1]-‐>Start(); TTask::WaitForAll(tasks,(sizeof(tasks)/sizeof(tasks[0])-‐1)); ShowMessage("All done!"); }
C++1
2 3
4 5
6 7
EMBARCADERO TECHNOLOGIES
9 Customizing the Thread Pool
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
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
EMBARCADERO TECHNOLOGIES
12 Futures - Calculating Values Asynchronously
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)
EMBARCADERO TECHNOLOGIES
Future Syntax
var FutureString: IFuture<string>; // . . . FutureString := TTask.Future<string>( function:string begin Result := DoRESTCall(editArtist.Text); end); // . . . ResultsMemo.Text := FutureString.Value;
OP1
2 3
4
5
EMBARCADERO TECHNOLOGIES
DEMONSTRATIONTasks and Futures
EMBARCADERO TECHNOLOGIES
16
Background by Tdvance used under CC-‐BY-‐SA 2.5 Font from http://www.dafont.com/back-‐to-‐the-‐future.font
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
EMBARCADERO TECHNOLOGIES
PPL Resources
• Samples – 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
• 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/
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
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/
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
EMBARCADERO TECHNOLOGIESEMBARCADERO TECHNOLOGIES
Q & A @EmbarcaderoTech
Special offers: http://embarcadero.com/radoffer/