writeln() sometimes double prints from main() if I run a thread checking for input?

Timothy Foster via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 30 03:13:57 PDT 2017


I'm not sure if this is a known issue, or if I just don't 
understand how to use threads, but I've got writeln statements 
sometimes printing out twice in some areas of my code. It seems 
to only happen when I start a thread that checks for input with 
stdin.byLineCopy (I'm not sure of the internal workings of it so 
that may be the issue)

The setup is basically this:

void main(){
     auto thread = Thread(&func).start;
     writeln("Output");
     ...
}

void func(){
     foreach (line; stdin.byLineCopy) {
         ...
     }
}

Some of the time it will print out "Output" and some of the time 
it will print out
"Output
Output"

Does anyone know what is causing this or how I can fix it?


More information about the Digitalmars-d-learn mailing list