dont understand this

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 05:38:20 PDT 2016


On 9/25/16 1:48 AM, collerblade wrote:
>> threadFunc contains a reference to MyClass in the form of the 'this'
>> pointer and so the GC sees this and never deallocates the MyClass
>> instance to begin with.
>
> Yes i am a ware about that fact, but when i set "running" to static, the
> solution WORKS. That is what the question is.

There is a misunderstanding here. It works because a static variable is 
*NOT* shared between threads.

i.e., make running static, then remove the set to false in the 
destructor, and you still will have the thread exit.

You cannot make a thread function that uses a delegate of a thread, 
which contains a pointer to that thread, and then expect the GC to kill 
the thread.

I know what you are trying to do, but I don't think you can do it with 
D's Thread objects. What you want to do is use a reference counter, and 
run the thread as long as the reference count is greater than 1. Then 
store your state in that object.

You can probably learn how to do this by reading the code for 
std.typecons.RefCounted.

-Steve


More information about the Digitalmars-d mailing list