www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.parallelism.taskPool daemon threads not terminating

reply Moritz Maxeiner <moritz ucworks.org> writes:
So, I am probably overlooking something obvious, but here goes:
According to my understanding of daemon threads and what is 
documented here[1],
this following program should terminate once the druntime shuts 
down, as the thread working on the task is supposed to be a 
daemon thread:

 import std.parallelism;
 
 void main()
 {
 	taskPool.put(task({ while(true) {} }));
 }
The actual behaviour (with dmd 2.071 and ldc2 1.0.0), however, is that the program keeps running. In contract, this behaves as expected:
 import core.thread;

 void main()
 {
    with (new Thread({ while(true) {} })) {
        isDaemon = true;
        start();
    }
}
Commenting out setting the isDaemon property will achieve the same behaviour as the taskPool example. Is this the intended behaviour of taskPool (because it does have isDaemon set)? [1] https://dlang.org/library/std/parallelism/task_pool.html
Jun 16 2016
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2016-06-17 at 00:14 +0000, Moritz Maxeiner via Digitalmars-d-
learn wrote:
 So, I am probably overlooking something obvious, but here goes:
 According to my understanding of daemon threads and what is=C2=A0
 documented here[1],
 this following program should terminate once the druntime shuts=C2=A0
 down, as the thread working on the task is supposed to be a=C2=A0
 daemon thread:
=20
 import std.parallelism;
=20
 void main()
 {
 	taskPool.put(task({ while(true) {} }));
 }
=20 The actual behaviour (with dmd 2.071 and ldc2 1.0.0), however, is=C2=A0 that the program keeps running.
A priori, assuming I am not missing anything, this behaviour seems entirely reasonable. The task is an infinite loop so it never terminates. This means the threadpool does not stop working, which means the program does not terminate.
 In contract, this behaves as expected:
=20
 import core.thread;
=20
 void main()
 {
 =C2=A0=C2=A0=C2=A0with (new Thread({ while(true) {} })) {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0isDaemon =3D true;
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0start();
 =C2=A0=C2=A0=C2=A0}
 }
=20 Commenting out setting the isDaemon property will achieve the=C2=A0 same behaviour as the taskPool example. Is this the intended=C2=A0 behaviour of taskPool (because it does have isDaemon set)?
I suspect that daemon may not mean what you think it means. At least not with respect to the threadpool.
=20
 [1] https://dlang.org/library/std/parallelism/task_pool.html
--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jun 17 2016
parent Moritz Maxeiner <moritz ucworks.org> writes:
On Friday, 17 June 2016 at 14:29:57 UTC, Russel Winder wrote:
 A priori, assuming I am not missing anything, this behaviour 
 seems entirely reasonable.
I agree that when using non-daemon threads (and I personally think that should be the default) that it is. But I cannot bring that into accord with the documentation of taskPool (the property)[1]:
 Returns a lazily initialized global instantiation of TaskPool.
 [...]
 The worker threads in this pool are daemon threads, meaning 
 that it is not necessary to call TaskPool.stop or 
 TaskPool.finish before terminating the main thread.
 A daemon thread is automatically terminated when all non-daemon 
 threads have terminated.
 A non-daemon thread will prevent a program from terminating as 
 long as it has not terminated.
The above - while not explicitly stating that daemon-threads do not prevent a program from terminating - are - to me - strongly suggesting it (and if they do indeed not, then I would ask how daemon threads are differnt from non-daemon threads in the context of TaskPool, since I'm unable to make it out from the documentation).
 The task is an infinite loop so it never terminates. This means 
 the threadpool does not stop working, which means the program 
 does not terminate.
Yes, that example is intentionally chosen that way to make my point. I initially discovered this while putting of a synchronous read of STDIN in a loop, but that example might have diverted attention to something other than I intended.
 I suspect that daemon may not mean what you think it means. At 
 least not with respect to the threadpool.
I do, too, which is why I asked here, since after having read the relevant documentation several times with significant time delay in between I still cannot make out how else to interpret it (and I got no reply in #dlang IRC). [1] https://dlang.org/library/std/parallelism/task_pool.html
Jun 17 2016