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:55:20 PDT 2017


On Wednesday, 30 August 2017 at 10:44:43 UTC, Ivan Kazmenko wrote:
> On Wednesday, 30 August 2017 at 10:13:57 UTC, Timothy Foster 
> wrote:
>> 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.
>> <...>
>> Does anyone know what is causing this or how I can fix it?
>
> Difficult to say by what you posted.
>
> You may want to provide a complete example so that others may 
> try to reproduce it.  Additionally, as you gradually simplify 
> your code until it is small enough to post here, or on DPaste, 
> you may find the cause faster yourself.
>
> Ivan Kazmenko.

import std.stdio, core.thread;

void main(){	
	auto thread = new Thread(&func).start;
         writeln("Output");
	writeln("Output2");
	writeln("Output3");
	while(true){}
}

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

The printout from the above typically gives me:
Output
Output2
Output2
Output3

I've narrowed down the issue. I just don't know what to do about 
it.


More information about the Digitalmars-d-learn mailing list