[D-runtime] A mechanism to kill threads

Alex Rønne Petersen xtzgzorex at gmail.com
Wed May 16 14:29:36 PDT 2012


The only reason I can think of is exactly what you mentioned, but this
is a non-issue because you *really* *should* *not* kill non-daemon
threads, and daemon threads should be designed to handle 'abnormal'
termination gracefully.

That said, I think it's okay to kill a thread which is just waiting
for work, sleeping, or whatever. As long as it isn't holding onto a
lock (or similar), you should be safe. Holding onto a resource like a
lock could easily lead to a deadlock because the lock is never
released when you kill the thread.

I think the TL;DR of all this is that you should really only kill
threads if either a) you know they can tolerate it (daemon threads) or
b) you know specifically what the thread is doing and that it's safe
to interrupt that work and never resume it (speaking of which, killing
a thread isn't really any different from suspending it and just never
resuming it).

Regards,
Alex

On Wed, May 16, 2012 at 11:19 PM, David Simcha <dsimcha at gmail.com> wrote:
> For the sake of argument, what are the most non-obvious reasons why killing
> threads is bad?  The ones I can think of are because the thread may be in
> the middle of doing something important and bad things will happen if it's
> interrupted and because the thread might hold resources that will never get
> freed if it's killed before it gets to free them.
>
> I was thinking at one point that I wanted a kill() primitive when I was
> designing std.parallelism, but I don't remember why I wanted it and
> obviously I've managed to do without it.  Is it ok to kill threads if
> they're not doing useful work at the time and they're not holding onto any
> resources that will never get freed if you kill them?
>
>
> On Wed, May 16, 2012 at 5:04 PM, Jonathan M Davis <jmdavisProg at gmx.com>
> wrote:
>>
>> On Wednesday, May 16, 2012 20:57:15 Alex Rønne Petersen wrote:
>> > Of course it's unsafe, but no less than arbitrarily suspending a
>> > thread. Once you've suspended a thread, you may as well kill it.
>> > You've effectively halted it anyway. We have lots of unsafe primitives
>> > in core.thread already (and my critical regions and cooperative
>> > suspension patches add even more!).
>> >
>> > Is there anything speaking against adding this to core.thread with a
>> > big fat "THIS IS UNSAFE" warning?
>>
>> Well, I wasn't saying that it's necessarily the case that we shouldn't add
>> it.
>> I was pointing out that it's a very unsafe thing to do and rarely needed
>> such
>> that it's not exactly surprising that it hasn't been added previously. If
>> there's a real use case for it, it makes sense to add it given that it
>> _is_
>> something that's platform dependent, and part of the whole point of Thread
>> is
>> to provide a platform-independent API for handling threads. It probably
>> _should_ have a big fat warning on it though, given that not all
>> developers
>> seem to get how insanely bad it is to kill a thread (at least, that seems
>> to
>> be the case given some thread-related questions I've seen in the past).
>>
>> - Jonathan M Davis
>> _______________________________________________
>> D-runtime mailing list
>> D-runtime at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/d-runtime
>
>
>
> _______________________________________________
> D-runtime mailing list
> D-runtime at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/d-runtime
>


More information about the D-runtime mailing list