February 17, 2012
I would like to get some feedback on a language feature I think would benefit D.

The feature is simplified "lazy" thread blocks that would considerably simplify the process of spawning and running new threads. Multiprocessing is becoming a more-and-more important feature for programmers as Moore's Law pushes us into a greater number of processors with only marginal speed improvements.

I think this feature could make it so much simpler to start and stop threads that many programmers that currently only multiprocess when necessary would, instead, do it any time they felt their software could benefit from it.

Anyhow, here's an example of my idea:

import std.stdio;
import core.thread;

void main() {
	runthread {
		Thread.sleep(dur!("msecs")(20));
		writeln("C");
	}
	threadA: runthread {
		Thread.sleep(dur!("msecs")(10));
		writeln("A");
	}

	sync threadA;
	writeln("B");
	sync;
	writeln("End");
}

For this example, the output would always be:
A
B
C
End

Internally, the compiler would convert the runthread blocks into something more-or-less like this:

import std.stdio;
import core.thread;

void main() {
	void doC() {
		Thread.sleep(dur!("msecs")(10));
		writeln("C");
	}
	Thread threadC = new Thread(&doC);
	threadC.start();

	void doA() {
		writeln("A");
	}
	Thread threadA = new Thread(&doA);
	threadA.start();

	threadA.join(); //sync threadA;
	writeln("B");
	threadC.join(); //sync all threads that haven't been join'd yet
	writeln("End");
}

For something as simple as printing text, the thread overhead isn't worth it; this is just an example. Imagine instead that you needed to run several queries against a database while also fetching a remote URL before using the results from all of those to display something to the user. This change would make doing those tasks in parallel trivial.

What are your thoughts? From a high level, does this seem that it would beneficial? Would it be worth my time to implement this?

- Jon
February 18, 2012
On Thu, 16 Feb 2012 20:38:37 -0600, Jonathan Stephens <slashrslashn@gmail.com> wrote:
> I would like to get some feedback on a language feature I think would benefit D.

Does std.parallelism fit your needs?