[D-runtime] A mechanism to kill threads

Alex Rønne Petersen xtzgzorex at gmail.com
Wed May 16 15:42:15 PDT 2012


The trouble is that they have to have version blocks for every
platform and every threading library supported. The fact that
core.thread supports pthreads should not even be known to code that
extends the Thread class; it's an implementation detail (other
threading libraries exist, especially on Linux). I believe there is
currently active work to make GDC support other C libraries, and I
imagine that involves other threading libraries on rather exotic
platforms too.

The way I've always seen core.thread is as a fundamental building
block for threading; it should hide the native threading interface
completely so people shouldn't have to mess with those at all.

Regards,
Alex

On Thu, May 17, 2012 at 12:36 AM, Sean Kelly <sean at invisibleduck.org> wrote:
> 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.
> _______________________________________________
> 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