Using std.stdio.File in a class
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 8 09:18:53 PDT 2014
On Tue, 08 Apr 2014 11:54:45 -0400, Spacen Jasset
<spacenjasset at mailrazer.com> wrote:
> On Tuesday, 8 April 2014 at 15:18:20 UTC, Steven Schveighoffer wrote:
>> Just a word of caution, I don't think the RAII semantics of File work
>> properly in multithreaded code when the File is destroyed by the GC.
>
> What do you mean by don't work? I can see that it won't be RAII anymore
> since there is no stack scope. However, I expect it to get closed
> properly - as and when the containing object is collected, or when you
> use the scope keyword with a class object.
>
> Is this not so?
The reference count inc/dec is not thread-safe, so if the GC collects your
object in a thread that is not the original thread, it may be racing with
your thread that's simultaneously inc/dec the reference count.
What can you do?
1. Single threaded program.
2. destroy the class deterministically with destroy(x);
3. Ensure the file reference is unique, do not share it with any other
objects.
-Steve
More information about the Digitalmars-d-learn
mailing list