Threads not garbage collected ?

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 22 04:18:11 PST 2017


Alex wrote:

> The thread can then prevent the program from exiting on exception or 
> otherwise.
> If the garbage collector doesn't kill threads, do I need to break all 
> encapsulation to call some sort of finalise or destroy function on 
> every object in case it has a thread object in it ?
> It would probably be better to have all core.thread.Threads 
> registered in the run time so they can be killed when main exits.
they are registered. but not exiting while at least one thread is alive 
is deliberate decision.

you may take a look at std.concurrency instead of working with "raw" 
threads. besides providing services like message passing, it does 
terminate all the spawned threads if their owner dies. you may change 
that, of course, this is just a convenient default.

but note that such termination will require calling `receive()` in a 
spawned thread. still, i think that message passing feature of 
std.concurrency is so useful that there is no reason to bypass it and 
invent your own. besides, you may just call `receiveTimeout()` with 
small timeout (not zero, there *was* a bug with zero == infinity) in 
your thread even if you aren't using standard message passing 
mechanics; it is completely harmless.


More information about the Digitalmars-d mailing list