GC Destruction Order

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 21 06:15:25 PDT 2015


On 5/21/15 8:40 AM, Kagamin wrote:
> On Thursday, 21 May 2015 at 12:33:33 UTC, Steven Schveighoffer wrote:
>> At this moment, a thread-local-only heap pointer must deal with
>> multi-threading issues simply because destructors can run on another
>> thread, even though the reference is thread-local. The biggest example
>> right now is reference-counted structures such as std.stdio.File.
>
> You mean the reference counting part?

Yes, if you put a File as a class member, the destructor of the class 
would call the File's destructor, which could be run in any thread. 
Since File is a reference counted wrapper, the destructor decrements the 
reference count and possibly closes the file. The whole operation must 
be atomic if we are spread across threads.

But if you only ever access that containing class in a local thread, you 
are paying the cost of locking (or at least atomic decrement) for all 
reference counts on that file, for very little reason. Especially if the 
File doesn't live in a heap object (dtor doesn't know where it's being 
called from).

-Steve


More information about the Digitalmars-d-learn mailing list