On Wednesday, 1 July 2020 at 07:52:28 UTC, AB wrote:
Hello. I am unsure how to proceed about printing progress in my program.

Suppose the program is processing a very big file and is iterating the file's bytes using a for loop. The processing takes several minutes and I want a progress percentage be printed every 2 seconds in this manner:

Progress: 0.40%
Progress: 3.20%
Progress: 5.73%

Is it a good idea to std.concurrency.spawn a new thread and pass to it
    cast(float)i * 100 / fileSize
somehow? If not, what's a better way to do this?

This example code shows my situation:

    MmFile  input       = new MmFile(/* ... */);
    ulong   fileSize    = input.length;

    for (ulong i = 0; i < fileSize; ++i)
        // ...

Thanks in advance.

If doing the update in the same thread that does the processing is somehow not an option, this works for me:

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

void main() {
    ulong filesize = 1234;
    ulong i = 0;
    Tid progress = spawn((shared const(ulong)* p, ulong f){
            while (!receiveTimeout(2000.msecs, (int i){ })) {
                writeln(*p, "/", f, ": ", *p*100.0/f, "%");
        }, cast(shared)&i, filesize);
    for (; i < filesize; ++i) {
        // Process
    progress.send(0); // Stop

There's a cast to shared there which may be suboptimal, but since the progress thread is only reading it, I would say it's ok.


Reply via email to