www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "Segmentation Fault" on ubuntu linux

hello,I use dmd2.029 on ubuntu linux.
Segmentation Fault accur when run the code
code:
module dsun.Fiber;
private import core.thread;
private import std.stdio;

private class SyncQueue(T)
{

    private T[] queue;

    public synchronized void push(T o)
    {
        queue ~= o;
    }

    public synchronized T pop()
    {
        T o = null;
        if(queue.length > 1){
            o = queue[0];
            queue = queue[1..$];
        }
        else if (queue.length == 0)
        {
            o = queue[0];
            queue = [];
        }
        return o;
    }
}

private class Scheduler
{
    private SyncQueue!(Fiber) runQueue;
    private SyncQueue!(Fiber) reusePool;
    private Thread[] threads;
    public this ()
    {
        runQueue  = new SyncQueue!(Fiber);
        reusePool = new SyncQueue!(Fiber);
    }

    public void addFiber(Fiber fiber)
    {
        runQueue.push(fiber);
    }

    public void run()
    {
        while (true)
        {
            Fiber fiber = runQueue.pop();
            if (fiber is null)
            {
                continue;
            }
            else
            {
                fiber.call();
                if (fiber.state == Fiber.State.TERM)
                {
                    fiber.reset();
                    reusePool.push(fiber);
                }
                else
                    runQueue.push(fiber);
            }
        }
    }

    public void schedule(int num = 4)
    {
        assert(num>0);
        for (int i=0; i< num; i++)
        {
            Thread t = new Thread(&run);
            threads ~= t;
            t.start();
        }

        for (int i=0; i< num; i++)
        {
            threads[i].join();
        }
    }
}

class WorkFiber:Fiber
{
    this(){
        super(&run);
    }

    void run()
    {
        while (true)
        {
          Fiber.yield();
        }
    }
}


void main()
{
    Scheduler p = new Scheduler;
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.addFiber(new WorkFiber);
    p.schedule(4);
}
May 09 2009