[fpc-devel] Suggestion: reference counted objects

Boian Mitov mitov at mitov.com
Mon Sep 22 20:54:16 CEST 2014


Here is example from our implementation:

for AIndex := 0 to Length( AList ) - 1 do
  AExecutionTask.Add(
      FExecutor.Execute(
          TDelegate.Capture<Integer>( AIndex,
              procedure( ALocalIndex : Integer )
              begin
                Sleep( 1000 ); // Simulate thread busy doing time consuming 
task
                AList[ ALocalIndex ] := ALocalIndex * 2;
              end
            )
        )
    );

AExecutionTask.WaitFor();


Notice the need for TDelegate :-( This is due to the fact that Delphi unlike 
C++11 does not allow the option to capture local context my value, and 
captures everything by reference :-( .
This is something to be considered when implementing anonymous methods or 
lambdas in FPC, especially for multithreading and parallel execution.

With best regards,
Boian Mitov

-------------------------------------------------------
Mitov Software
www.mitov.com
-------------------------------------------------------
-----Original Message----- 
From: Michael Schnell
Sent: Monday, September 22, 2014 5:58 AM
To: fpc-devel at lists.freepascal.org
Subject: Re: [fpc-devel] Suggestion: reference counted objects

On 09/22/2014 02:51 PM, Sven Barth wrote:
>
> Then you should also not use interfaces, strings and arrays, because they 
> use the same mechanisms.
>
Yep. The advanced developer should know what he is doing.
>
> What us confusing there?
>
Of course I do know the pitfalls.

If fpc would provide a syntax for "parallel loop" and "future variables"
e.g. similar to what Prism offers, even more inexperienced users will be
inclined to use (invisible) threads. I suppose it's not possible to
avoid the "confusion" that is introduced by this, but it should be
limited as much as possible. (No idea how.)

-Michael
_______________________________________________
fpc-devel maillist  -  fpc-devel at lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel 




More information about the fpc-devel mailing list