[D-runtime] A mechanism to kill threads

Sean Kelly sean at invisibleduck.org
Wed May 16 15:36:32 PDT 2012


On May 16, 2012, at 2:51 PM, Alex Rønne Petersen wrote:

> I'm sure it could be done outside of core.thread, but then anyone who
> legitimately knows what they're doing and needs it would have to
> reinvent the wheel.

But it's trivial in this case:

class KillableThread : Thread {
    this(void function() fn) {
        m_fn = fn;
        super(run);
    }

    public kill() {
        pthread_cancel(m_thisThread);
    }

    private void run() {
        m_thisThread = pthread_self();
        fn();
    }

    private pthread_t m_thisThread;
    private void function() m_fn;
}


Which I suppose could be made even easier by adding a protected method to Thread that returns the thread handle.


More information about the D-runtime mailing list