Threads not garbage collected ?

ketmar via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 22 00:28:48 PST 2017


Alex wrote:

> import core.thread;
>
> class A
> {
> 	Thread mThread;
> 	bool mStopped;
> 	 this()
> 	{
> 		mThread = new Thread(&run);
> 		mThread.start();
> 	}
> 	 void run()
> 	{
> 		while (!mStopped)
> 		{
> 			//do stuff
> 		}
> 	}
> 	~this()
> 	{
> 		mStopped = true;
> 		mThread.join();
> 	}
> }
this code is invalid. when `~this()` is called, your `mThread` *may* 
already be dead -- this is how GC works. you *may* *not* access 
heap-allocated members in `~this()`, this is a bug. it may work 
sometimes, but it isn't guaranteed.


More information about the Digitalmars-d mailing list