Futures in the OTL were not planned – they just happened. In fact, they are so new that you won’t find them in the SVN. (Don’t worry, they’ll be committed soon.)
As a matter of fact, I always believed that futures must be supported by the compiler. That changed few weeks ago when somebody somewhere (sorry, can’t remember the time and place) asked if they can be implemented in the OmniThreadLibrary. That question made me rethink the whole issue and I found out that not only it’s possible to implement them without changing the compiler – the implementation is almost trivial!
In the OTL 2.0 you’ll be able to declare a future …
var
numPrimes: IOmniFuture<integer>;
… start the evaluation …
numPrimes := TOmniFuture<integer>.Create(a delegate returning integer);
… and wait on the result.
numPrimes.Value
As simple as that. Declare the IOmniFuture<T>, create TOmniFuture<T> and retrieve the result by calling Value: T.
As a real-world example, the code below creates a future that calculates number of primes from 1 to CPrimesHigh and displays this value.
var
numPrimes: IOmniFuture<integer>;
begin
numPrimes := TOmniFuture<integer>.Create(function: integer
var
i: integer;
begin
Result := 0;
for i := 1 to CPrimesHigh do
if IsPrime(i) then
Inc(Result);
end
);
// do something else
lbLog.Items.Add(Format('%d primes from 1 to %d',
[numPrimes.Value, CPrimesHigh]));
end;
As a general rule, I would recommend against putting too much of the code inside the future’s constructor. A following approach is more readable and easier to maintain.
function CountPrimesToHigh(high: integer): integer;
var
i: integer;
begin
Result := 0;
for i := 1 to CPrimesHigh do
if IsPrime(i) then
Inc(Result);
end;
var
numPrimes: IOmniFuture<integer>;
begin
numPrimes := TOmniFuture<integer>.Create(function: integer
begin
Result := CountPrimesToHigh(CPrimesHigh);
end
);
// do something else
lbLog.Items.Add(Format('%d primes from 1 to %d',
[numPrimes.Value, CPrimesHigh]));
end;
Or you can take another step and create a future factory. That’s especially recommended if you’ll be using futures of the same kind in different places.
function StartCountingPrimesTo(high: integer): TOmniFuture<integer>;
begin
Result := TOmniFuture<integer>.Create(function: integer
var
i: integer;
begin
Result := 0;
for i := 1 to high do
if IsPrime(i) then
Inc(Result);
end
);
end;
var
numPrimes: IOmniFuture<integer>;
begin
numPrimes := StartCountingPrimesTo(CPrimesHigh);
// do something else
lbLog.Items.Add(Format('%d primes from 1 to %d',
[numPrimes.Value, CPrimesHigh]));
end;
Implementation
Believe it or not, the whole implementation fits in 27 lines (not counting empty lines).
type
TOmniFutureDelegate<T> = reference to function: T;
IOmniFuture<T> = interface
function Value: T;
end; { IOmniFuture<T> }
TOmniFuture<T> = class(TInterfacedObject, IOmniFuture<T>)
private
ofResult: T;
ofTask : IOmniTaskControl;
public
constructor Create(action: TOmniFutureDelegate<T>);
function Value: T;
end; { TOmniFuture<T> }
constructor TOmniFuture<T>.Create(action: TOmniFutureDelegate<T>);
begin
ofTask := CreateTask(procedure (const task: IOmniTask)
begin
ofResult := action();
end,
'TOmniFuture action').Run;
end; { TOmniFuture<T>.Create }
function TOmniFuture<T>.Value: T;
begin
ofTask.Terminate;
ofTask := nil;
Result := ofResult;
end; { TOmniFuture<T>.Value }
As you can see, the whole OTL task support is only used to simplify background thread creation. It would be quite simple to implement futures around Delphi’s own TThread. In fact, I think I’ll just go ahead and implement it!
Hi, I'm very impressed by OTL, good job. :)
ReplyDeleteI've a question about Parallel and Delphi 2009. Some things have to be compiled in D2010 only. I have D2009 and cannot compile sample app_35_parallelFor because of TOmniValue.CastFrom accessible for Compiler >= 21. Is it temporary state? Would this feature be supported by D2009?
Thanks a lot for your work.
@George: The plan is to fully support Parallel extensions in Delphi 2009 and better. This is just a temporary state resulting from the fact that a) I'm developing Parallel in Delphi 2010 and b) the Delphi 2009 on my machine is currently broken.
ReplyDeleteThe code in the trunk has now been updated and compiles&works in Delphi 2009. Parallel support is only slightly limited (compared to D2010) - partially because the lack of the extended RTTI support and partially because of the lack of the TValue type. In most cases this won't bother you (I hope).
ReplyDeletegabr, thanks, thanks, thanks (no, I'm not teenager :) )!!!
ReplyDelete