www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writeln() sometimes double prints from main() if I run a thread

reply Timothy Foster <timfost aol.com> writes:
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?
Aug 30 2017
parent reply Ivan Kazmenko <gassa mail.ru> writes:
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.
Aug 30 2017
parent reply Timothy Foster <timfost aol.com> writes:
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.
Aug 30 2017
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster 
wrote:
 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.
I cannot reproduce this. what os / compiler are you using ?
Aug 30 2017
parent Timothy Foster <timfost aol.com> writes:
On Wednesday, 30 August 2017 at 11:28:49 UTC, Stefan Koch wrote:
 On Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster 
 wrote:
 On Wednesday, 30 August 2017 at 10:44:43 UTC, Ivan Kazmenko 
 wrote:
 [...]
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.
I cannot reproduce this. what os / compiler are you using ?
Windows 7 Ultimate 64-bit DMD32 D Compiler v2.075.1 The issue is inconsistent. Sometimes it'll print as it should, sometimes it'll double the first line, sometimes the second, sometimes both the first and second.
Aug 30 2017
prev sibling parent reply Ivan Kazmenko <gassa mail.ru> writes:
On Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster 
wrote:
 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){}
 }
I also cannot reproduce this. My current system is Win7 64-bit. I tried 32-bit dmd 2.072.0 and 2.075.1, with optimizations turned on and off, but it prints correctly tens of times in each case. Try running the program in a bare console (cmd.exe on Windows, or some *sh on Linux). If the problem goes away, your usual environment is likely at fault. If not,.. well, no idea for now. Ivan Kazmenko.
Aug 30 2017
parent reply Ivan Kazmenko <gassa mail.ru> writes:
On Wednesday, 30 August 2017 at 13:24:55 UTC, Ivan Kazmenko wrote:
 On Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster 
 wrote:
 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){}
 }
I also cannot reproduce this. My current system is Win7 64-bit. I tried 32-bit dmd 2.072.0 and 2.075.1, with optimizations turned on and off, but it prints correctly tens of times in each case. Try running the program in a bare console (cmd.exe on Windows, or some *sh on Linux). If the problem goes away, your usual environment is likely at fault. If not,.. well, no idea for now.
Hey, I followed my own advice and do see the same issue! when: 1. compiling with dmd 2.075.1 (optimization switches seem to be irrelevant but the issue does not reproduce with 2.072.0), 2. running in a bare cmd.exe, and 3. running the program as just "a.exe", so that it waits for console input (previously I redirected some input to it, like "a.exe <a.exe" or "a.exe <a.d", and the issue does not reproduce then). Interesting. As to what to do with it, no idea for now. At the very least we can issue a bug report, now that at least two people can reproduce it, so it is unlikely to be environment-dependent. Ivan Kazmenko.
Aug 30 2017
next sibling parent Ivan Kazmenko <gassa mail.ru> writes:
On Wednesday, 30 August 2017 at 13:33:06 UTC, Ivan Kazmenko wrote:
 Interesting.  As to what to do with it, no idea for now.  At 
 the very least we can issue a bug report, now that at least two 
 people can reproduce it, so it is unlikely to be 
 environment-dependent.
Reported: https://issues.dlang.org/show_bug.cgi?id=17797 .
Aug 31 2017
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/30/17 9:33 AM, Ivan Kazmenko wrote:
 On Wednesday, 30 August 2017 at 13:24:55 UTC, Ivan Kazmenko wrote:
 On Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster wrote:
 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){}
 }
I also cannot reproduce this. My current system is Win7 64-bit.  I tried 32-bit dmd 2.072.0 and 2.075.1, with optimizations turned on and off, but it prints correctly tens of times in each case. Try running the program in a bare console (cmd.exe on Windows, or some *sh on Linux).  If the problem goes away, your usual environment is likely at fault.  If not,.. well, no idea for now.
Hey, I followed my own advice and do see the same issue! when: 1. compiling with dmd 2.075.1 (optimization switches seem to be irrelevant but the issue does not reproduce with 2.072.0), 2. running in a bare cmd.exe, and 3. running the program as just "a.exe", so that it waits for console input (previously I redirected some input to it, like "a.exe <a.exe" or "a.exe <a.d", and the issue does not reproduce then). Interesting.  As to what to do with it, no idea for now.  At the very least we can issue a bug report, now that at least two people can reproduce it, so it is unlikely to be environment-dependent.
Just a thought, but the "double printing" could be a misunderstanding. It could be printing Output\nOutput2, but not getting the 2 out there. Note that DMD 32-bit is using DMC libc. It might be that it gets hung up somehow when expecting input, like it locks the console somehow. I would say that byLineCopy puts the thread to sleep waiting for input, and it doesn't get out of that state. So it could be that the bug only appears when it gets to that state at some point in the output. I'd pepper some sleeps around the outputs to see if you can make the context switches more predictable. -Steve
Aug 31 2017
parent reply Ivan Kazmenko <gassa mail.ru> writes:
On Thursday, 31 August 2017 at 14:43:39 UTC, Steven Schveighoffer 
wrote:
 Just a thought, but the "double printing" could be a 
 misunderstanding. It could be printing Output\nOutput2, but not 
 getting the 2 out there.
No no, it's four lines instead of three. If we change the lines to disjoint sets of letters, the problem persists.
 Note that DMD 32-bit is using DMC libc. It might be that it 
 gets hung up somehow when expecting input, like it locks the 
 console somehow.

 I would say that byLineCopy puts the thread to sleep waiting 
 for input, and it doesn't get out of that state. So it could be 
 that the bug only appears when it gets to that state at some 
 point in the output. I'd pepper some sleeps around the outputs 
 to see if you can make the context switches more predictable.
Inserting different sleeps into the threads makes the problem go away (cannot reproduce). Inserting identical sleeps produces the error with roughly the same probability. Anyway, I've reported it (https://issues.dlang.org/show_bug.cgi?id=17797), along with a more or less exact version (2.073.2) where the problem was introduced. Ivan Kazmenko.
Aug 31 2017
parent John Burton <john.burton jbmail.com> writes:
On Thursday, 31 August 2017 at 21:06:17 UTC, Ivan Kazmenko wrote:
 On Thursday, 31 August 2017 at 14:43:39 UTC, Steven 
 Schveighoffer wrote:
 Just a thought, but the "double printing" could be a 
 misunderstanding. It could be printing Output\nOutput2, but 
 not getting the 2 out there.
Just to say that I can reproduce this on my system. Newly downloaded copy of v2.076.0 Window 7 "enterprise" 64 bit. Standard cmd.com command shell. I get Output2 duplicated most of the time. Occasionally I get Output duplicated. Occasionally Output2 is out of order...
Sep 05 2017