[fpc-devel] Suggestion: reference counted objects

hinstance hinstance at yandex.ru
Mon Sep 22 22:17:52 CEST 2014


I suggest doing one thing at a time. ARC class first, complex delegates 
capturing local context later

In my opinion having ARC class would be a great advantage. As far as I 
know this feature was proposed many times in the past. Developers have 
been asking for it for years. It just feels right because in FPC we 
already have ARC-interfaces, ARC-dynamic arrays and ARC-strings. And the 
Variant type, which is also ARC. In my opinion, implementing ARC for 
class type is both easier to implement and more important than anonymous 
methods. Anonymous methods save you typing, but that's it. ARC objects 
not only save you typing, but they also save you from many mistakes you 
can make while inserting .AddRef and .Release calls everywhere by hand.

I am not even sure if weak reference feature is necessary. We don't have 
weak references for COM interfaces, and it still works so far. Well, of 
course, if we had weak references as well, it would be a useful feature 
perhaps, but considering just *how many times* ARC class was proposed 
before, I lost hope that it will be ever implemented at all quite a 
while ago

Some people thing that GC is better than ARC. However ARC has at least 
one major advantage: unlike GC, it does not cause stop of the world. 
Complex Java programs often suffer from stop of the world because it may 
take considerable amount of time, and the application can't do anything 
during that time. For example, it can't respond to HTTP requests. If it 
is a game, even 200 ms freeze would be annoying. Well, you got the idea. 
ARC does not have to stop threads to collect garbage.

22.09.2014 22:54, Boian Mitov:
> 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
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20140923/64e8d60a/attachment.html>


More information about the fpc-devel mailing list