arlo on 2 Jul 2003 17:08:01 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [ALACPP] smart pointers in C


> In many cases where you would want to use smart pointers in C, a
> garbage collector will work just as well or better. See
> http://www.hpl.hp.com/personal/Hans_Boehm/gc/

For GC in C++, I prefer the templated smart pointer implementation found at
http://www.codeproject.com/cpp/garbage_collect2.asp?df=100. It is an
implementation of the usual thread safe, exception safe, mark-and-sweep gc.

I prefer this one on the basis of syntax: it creates a GC_ptr and a placement
new override, so you use syntax almost identical to boost's shared_ptr:

gc_ptr<base> foo(new(gc) derived(asf, fds, 10));
...
foo.reset(new(gc) derived2(asfd));
...
base bar;   // regular stack variable, not messed with by gc.
shared_ptr<base> baz(new base);     // non-gc heap variable.

Unfortunately, it is currently Windows-only. The GC is actually
cross-platform, but it uses platform-specific critical sections. It should be
a simple port (one lock class) to other platforms, which I'll likely do
eventually. ;)

There are currently a few issues with his implementation, which I'm working
on. The main two are performance issues: he holds locks too long, and he's got
one bad search algorithm: a linear search in an inner loop. All of his other
searches are good. Weird.

I haven't had a chance to run a speed trial against shared_ptrs. The GC will
be slower on average and have much worse worst-case behavior (when an alloc
triggers a collect), but I don't know how much. This gc does work with a
parallel gc approach (separate thread for performing collections), which could
help with the worst-case behavior. However, I don't know how much.

Likewise, I don't know if my performance issues are actually relevant. I'll be
examining that after I get a compiler set up again, and before I make any
changes.

Arlo


_______________________________________________
alacpp mailing list
alacpp@xxxxxxxxxxx
http://lists.ellipsis.cx/mailman/listinfo/alacpp