digitalmars.D.learn - writeln() sometimes double prints from main() if I run a thread
- Timothy Foster (22/22) Aug 30 2017 I'm not sure if this is a known issue, or if I just don't
- Ivan Kazmenko (8/13) Aug 30 2017 Difficult to say by what you posted.
- Timothy Foster (19/32) Aug 30 2017 import std.stdio, core.thread;
- Stefan Koch (3/40) Aug 30 2017 I cannot reproduce this. what os / compiler are you using ?
- Timothy Foster (6/35) Aug 30 2017 Windows 7 Ultimate 64-bit
- Ivan Kazmenko (10/21) Aug 30 2017 I also cannot reproduce this.
- Ivan Kazmenko (14/37) Aug 30 2017 Hey, I followed my own advice and do see the same issue! when:
- Ivan Kazmenko (2/6) Aug 31 2017 Reported: https://issues.dlang.org/show_bug.cgi?id=17797 .
- Steven Schveighoffer (11/47) Aug 31 2017 Just a thought, but the "double printing" could be a misunderstanding.
- Ivan Kazmenko (12/23) Aug 31 2017 No no, it's four lines instead of three. If we change the lines
- John Burton (6/11) Sep 05 2017 Just to say that I can reproduce this on my system. Newly
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
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
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: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'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
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:I cannot reproduce this. what os / compiler are you using ?On Wednesday, 30 August 2017 at 10:13:57 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){} } 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'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
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: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.On Wednesday, 30 August 2017 at 10:44:43 UTC, Ivan Kazmenko wrote:I cannot reproduce this. what os / compiler are you using ?[...]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
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
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: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.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.
Aug 30 2017
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
On 8/30/17 9:33 AM, Ivan Kazmenko wrote:On Wednesday, 30 August 2017 at 13:24:55 UTC, Ivan Kazmenko 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. 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. -SteveOn Wednesday, 30 August 2017 at 10:55:20 UTC, Timothy Foster wrote: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.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.
Aug 31 2017
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
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 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...Just a thought, but the "double printing" could be a misunderstanding. It could be printing Output\nOutput2, but not getting the 2 out there.
Sep 05 2017