Language idea - simple thread spawning
Jacob Carlborg
doob at me.com
Fri Feb 17 06:36:16 PST 2012
On 2012-02-17 11:35, Artur Skawina wrote:
> On 02/17/12 03:38, Jonathan Stephens wrote:
>> 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
>>
> [...]
>> What are your thoughts? From a high level, does this seem that it
>> would beneficial? Would it be worth my time to implement this?
>
> Would it really be an significant improvement? Compare the above with this:
>
> -----------------------------------------------------
> import std.stdio;
> import core.thread;
>
> void main() {
> new thread({
> Thread.sleep(dur!("msecs")(20));
> writeln("C");
> });
> auto threadA = new thread({
> Thread.sleep(dur!("msecs")(10));
> writeln("A");
> });
>
> threadA.join();
> writeln("B");
> thread.sync();
> writeln("End");
> }
> -----------------------------------------------------
If the language supported to pass a delegates after the function call if
the function takes a delegate as the last argument (which has been
discussed before), something like this:
void foo (void delegate () dg);
foo {
// this is the delegate passed to "foo"
}
Then it would be possible to implement "runthread" as a function taking
a delegate.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list