好​​像如果你这样做,你的问题就解决了。或者,你可以创建一个工厂函数来创建B对象,返回一个 smartptr< B> ;. Tom 也许在实例中放置一个标志B表示实例是否已将 分配给任何smartptr,并更改smartptr< B> :: operator =以设置 此标志。然后在分配之前不要收集B的实例。 当然,真正的GC通过扫描堆栈和寄存器来避免这个问题, 和使用GC安全点。 - David Hopwood< da **************** **@blueyonder.co.uk> 也许在B的实例中放置一个标志,表示是否已将实例分配给任何smartptr,并更改smartptr< B> :: operator =以设置此标志。然后在分配之前不要收集B的实例。 当然,真正的GC通过扫描堆栈和寄存器,以及使用GC安全点来避免这个问题。 / blockquote> 什么是GC安全点(我有点想法)以及它们如何与GC进行交互?b $ b?我知道怎么做并发GC,但我想知道传统的 方法是什么。 Joe Seigh I am trying to create a garbage collection class in C++ to collectinstances of a specific class (so no general gc). The approach is touse 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 withthe garbage collector. The constructor of smartptr has also beenadjusted to register new instances of smart pointers with the garbagecollector.To collect, the gc now iterates all known smart pointers and marks thereferenced objects as active. After this, it iterates all known objectsand deletes all thhose that have not been marked active. The gc will becalled explicitly. This works fine so far when using a single thread. However, when usingmultiple 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 threadruns the gc? It will find the new created object not (yet) referencedand delete it. What would be the proper solution to prevent this? At a first thought,you might come togc.lock();pB = new B(args);gc.unlock(); But then considerB* f(...){...return new B(args);} ...pB = f(...); Here, construction and assignement are not on one line. Thus using thelock strategy will not work. Wrapping the whole function call withlock/unlock will result in bad performance. (In fact, the reason of using a gc like this is a little morecomplicated. At the bottom line: no I cannot move to reference countingor one of the smart pointer implementations). I am looking for any suggestions. 解决方案 Seems like if you do the above, your problem is solved. Alternatively,you could create a factory function to create B objects that returns asmartptr<B>. Tom Perhaps put a flag in instances of B that indicates whether the instance hasbeen assigned to any smartptr yet, and change smartptr<B>::operator= to setthis flag. Then don''t collect instances of B until they have been assigned. Of course real GCs avoid this problem by scanning the stack and registers,and by using GC safe-points. --David Hopwood <da******************@blueyonder.co.uk> Perhaps put a flag in instances of B that indicates whether the instance has been assigned to any smartptr yet, and change smartptr<B>::operator= to set this flag. Then don''t collect instances of B until they have been assigned. Of course real GCs avoid this problem by scanning the stack and registers, and by using GC safe-points. What''s a GC safe-point (I sort of have an idea) and how do they interact withthe GC? I know how to do concurrent GC but I''m wondering what the conventionalapproach is. Joe Seigh 这篇关于线程问题(构造函数+分配一步?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 07:58