Artifact 5655f25b59cc69ba4857e6de38f0fbd77ad2b36ca82311254dd527f12cbcf9ef:

  • File par/1-spinner/spinner.d — part of check-in [40a2744c9d] at 2021-10-30 19:24:22 on branch trunk — add 'spinner' example from Go Programming Language ch8 golang works 2.18x as hard (perf stat cycle count) to take 2.31x times as long (hyperfine runtime) as spinner.gdc. (user: admin size: 586)

import std.stdio;
import std.concurrency;
import core.thread;

void main() {
    spawnLinked(&spinner, 100.msecs);
    enum n = 45;
    auto fibN = fib(n); // slow
    writefln!"\rFibonacci(%d) = %d"(n, fibN);
}

void spinner(const Duration delay) {
    while (true) {
        bool done;
        foreach (r; `-\|/`) {
            writef!"\r%c"(r);
            stdout.flush;
            receiveTimeout(delay, (OwnerTerminated msg) { done = true; });
            if (done)
                return;
        }
    }
}

int fib(int x) pure {
    return x < 2 ? x : fib(x - 1) + fib(x - 2);
}