Fibers

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 17 03:00:47 PDT 2014


The script below is borrowed form a unit test in core.thread and 
modified slightly. If fails with "segmentation fault: 11" but I'm not 
sure why.

Basically what I'm trying to do is to transact on every file in give 
directory at the same time exact time.

In this exam, I'm removing the file/directory but that's just a test to 
see if I could get it to work.

Guidance is requested and appreciated.

Thanks,
Andrew

---------------------------------
import core.thread;
import std.process;
import std.file;
import std.stdio;

void main(string[] args)
{
	auto f = args[1];
	deleteWithFibers(f);
}

class TestFiber : Fiber
{
	string file;

	this(string file)
	{
		this.file = file;
		super(&run);
	}

	void run()
	{
		while (file.exists) {
			if (file.isFile)
				file.remove;
			else if (file.isDir)
				file.rmdir;
			Fiber.yield();
		}
	}
}

void deleteWithFibers(string dir)
{
	auto fibs = new TestFiber[100];
	
	int count = 0;
	foreach (entry; dirEntries(dir, SpanMode.depth))
	{
		fibs[count] = new TestFiber(entry);

		if (++count > fibs.length)
			fibs.length *= 2;
	}

	fibs.length = count+1;

	bool cont;
	do {
		cont = false;
		foreach(fib; fibs) {
			if (fib.state == Fiber.State.HOLD)
			{
				fib.call();
				cont |= fib.state != Fiber.State.TERM;
			}
		}
	} while (cont);
}


More information about the Digitalmars-d-learn mailing list