Y
yuraukar
I am trying to create a garbage collection class in C++ to collect
instances of a specific class (so no general gc). The approach is to
use smart pointers and a mark and a simple sweep gc.
smartptr<B> pB;
pB = new B(args);
The constructor of B has been adjusted to register new instances with
the garbage collector. The constructor of smartptr has also been
adjusted to register new instances of smart pointers with the garbage
collector.
To collect, the gc now iterates all known smart pointers and marks the
referenced objects as active. After this, it iterates all known objects
and deletes all thhose that have not been marked active. The gc will be
called explicitly.
This works fine so far when using a single thread. However, when using
multiple threads, there is a problem.
The assignement above is essentially two steps. First create the object,
then do the assignement. What if between these two steps another thread
runs the gc? It will find the new created object not (yet) referenced
and delete it.
What would be the proper solution to prevent this? At a first thought,
you might come to
gc.lock();
pB = new B(args);
gc.unlock();
But then consider
B* f(...)
{
...
return new B(args);
}
...
pB = f(...);
Here, construction and assignement are not on one line. Thus using the
lock strategy will not work. Wrapping the whole function call with
lock/unlock will result in bad performance.
(In fact, the reason of using a gc like this is a little more
complicated. At the bottom line: no I cannot move to reference counting
or one of the smart pointer implementations).
I am looking for any suggestions.
instances of a specific class (so no general gc). The approach is to
use smart pointers and a mark and a simple sweep gc.
smartptr<B> pB;
pB = new B(args);
The constructor of B has been adjusted to register new instances with
the garbage collector. The constructor of smartptr has also been
adjusted to register new instances of smart pointers with the garbage
collector.
To collect, the gc now iterates all known smart pointers and marks the
referenced objects as active. After this, it iterates all known objects
and deletes all thhose that have not been marked active. The gc will be
called explicitly.
This works fine so far when using a single thread. However, when using
multiple threads, there is a problem.
The assignement above is essentially two steps. First create the object,
then do the assignement. What if between these two steps another thread
runs the gc? It will find the new created object not (yet) referenced
and delete it.
What would be the proper solution to prevent this? At a first thought,
you might come to
gc.lock();
pB = new B(args);
gc.unlock();
But then consider
B* f(...)
{
...
return new B(args);
}
...
pB = f(...);
Here, construction and assignement are not on one line. Thus using the
lock strategy will not work. Wrapping the whole function call with
lock/unlock will result in bad performance.
(In fact, the reason of using a gc like this is a little more
complicated. At the bottom line: no I cannot move to reference counting
or one of the smart pointer implementations).
I am looking for any suggestions.