<div dir="ltr"><div><br><div class="gmail_quote">On Sat, Aug 24, 2013 at 3:26 PM, bearophile <span dir="ltr"><<a href="mailto:bearophileHUGS@lycos.com" target="_blank">bearophileHUGS@lycos.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
David Nadlinger:<div><br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
It's a cute idea, but not really practical, I'm afraid – Goroutines are
managed by a scheduler in the Go runtime library, whereas D threads
directly map to OS threads.<br>
</blockquote>
<br></div>
Can't Rory McGuire add a scheduler to his code? How much code does it take?<br>
<br>
Bye,<br>
bearophile<br>
</blockquote></div><br><br>Here is a basic co-operative scheduler based version of go!(F) that uses a channel to keep things simple, its co-op so Fiber.yield has to be called at some point. If the channel detects its in a Fiber it calls yield if there is nothing available in it:<br>
<br>void go(alias F)() {<br> scheduler._ = new Fiber(F);<br>}<br><br>shared chan!Fiber scheduler; // channel contains Fibers waiting for their time slice<br>
static this () {<br> if (scheduler is null) { <br> scheduler = makeChan!Fiber(100);<br><br> // create the workers<br> auto goprocs = environment.get("GOPROCS");<br> int num_threads = 1;<br>
if (goprocs != null) {<br> num_threads = to!int(goprocs);<br> }<br> foreach (i; 0..num_threads) {<br> // create threads that process the live fibers<br> auto t = new Thread(() {<br>
for (;;) {<br> auto fiber = scheduler._;<br> fiber.call();<br> if (fiber.state != Fiber.State.TERM) {<br> scheduler._ (fiber);<br>
}<br> }<br> });<br> t.start();<br> }<br> <br> }<br>}<br><br></div>Just need to figure out a way to detect when main() has exited.<br></div>