| Thread overview | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
On Thursday, February 16, 2012 19:38:37 Jonathan Stephens wrote: > What are your thoughts? From a high level, does this seem that it would beneficial? Would it be worth my time to implement this? Have you looked at std.concurrency or read this chapter in TDPL (which is one of the two chapters free online)? http://www.informit.com/articles/article.aspx?p=1609144 - Jonathan M Davis | ||||
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
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"); } ----------------------------------------------------- which works right now [1]. artur [1] with a trivial "thread" class like the one below. Note it's just POC, you may not want to implement it like this. Oh, i used "thread" to keep the diff to your example as small as possible, before someone mentions that it must use CamelCasing. :^) class thread: Thread { static Thread[typeof(this)] threads; this(void function() f) { auto t = super(f); threads[this] = t; t.start(); } this(void delegate() f) { auto t = super(f); threads[this] = t; t.start(); } void join() { super.join(); threads.remove(this); } static sync() { foreach(t; threads) t.join(); } } | ||||
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
On 17 February 2012 10:35, Artur Skawina <art.08.09@gmail.com> wrote: > > [1] with a trivial "thread" class like the one below. Note it's just POC, you may not want to implement it like this. Oh, i used "thread" to keep the diff to your example as small as possible, before someone mentions that it must use CamelCasing. :^) > Actually, I believe that's called PascalCasing. camelCasing starts with a lower character to denote the head of the camel. ;-) -- Iain Buclaw *(p < e ? p++ : p) = (c & 0x0f) + '0'; | ||||
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
On 02/17/12 11:40, Iain Buclaw wrote:
> On 17 February 2012 10:35, Artur Skawina <art.08.09@gmail.com> wrote:
>>
>> [1] with a trivial "thread" class like the one below. Note it's just POC, you may not want to implement it like this. Oh, i used "thread" to keep the diff to your example as small as possible, before someone mentions that it must use CamelCasing. :^)
>>
>
> Actually, I believe that's called PascalCasing.
>
> camelCasing starts with a lower character to denote the head of the camel. ;-)
Makes sense, thanks for the correction.
As long as it's not called DCasing I can live with that. :)
artur
| ||||
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
Also, the current bog-standard way to spawn threads is easy enough
spawn(function () {
// do things
});
admittedly the processing of stopping a thread is a bit trickier, but not much. And artur's example shows a simple way of doing it. This syntax won't really give us anything we can't already do.
--
James Miller
| ||||
February 17, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | 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 | |||
February 18, 2012 Re: Language idea - simple thread spawning | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | > Have you looked at [http://www.informit.com/articles/article.aspx?p=1609144] Yes, I read the first three pages then looked up the Thread API before I realizing what it wanted to teach me. (Probably this has taught me something. I'm not sure what it is.) You all make great points, and I apologize for being ignorant of how close we already were. That said, I still yearn for the day when the "normal" programming languages treat multiprocessing as a core language concept and not just a (standard) library you pass closures/delegates/interfaces to. > foo { > // this is the delegate passed to "foo" > } > > Then it would be possible to implement "runthread" as a function taking a delegate. If working with Lua has taught me anything, it's that languages with optionally omitted parenthesis are annoying. ;-) | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply