www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange Thread Causing Duplicating `writeln`

reply Jonathan <JonathanILevi gmail.com> writes:
I am totally lost on why this is happening.

I stripped the code down to what appears to be the most minimal 
code that still causes the problem.

---
import core.sync.mutex;
import core.thread;
import std.stdio;

__gshared Mutex m;//__gshared just for testing (;

void thread1() {
	foreach (i;0..8) {
		synchronized(m) {
			writeln("a1-",i);
		}
		writeln("a2-",i);
	}
}
void thread2() {
	foreach (i;0..8) {
		synchronized(m) {
			writeln("b1-",i);
		}
		writeln("b2-",i);
	}
}


void main() {
	m = new Mutex();
	
	new Thread(&thread1).start;
	new Thread(&thread2).start;
}
---
The beginning of the output for this code is:
a1-0
a2-0
a2-0
b1-0
b2-0
b2-0
a1-1
a2-1
a2-1

Why is the "a2" and "b2" writeln being repeated?!
Apr 09 2018
parent reply Cym13 <cpicard openmailbox.org> writes:
On Monday, 9 April 2018 at 22:28:45 UTC, Jonathan wrote:
 I am totally lost on why this is happening.

 I stripped the code down to what appears to be the most minimal 
 code that still causes the problem.

 ---
 import core.sync.mutex;
 import core.thread;
 import std.stdio;

 __gshared Mutex m;//__gshared just for testing (;

 void thread1() {
 	foreach (i;0..8) {
 		synchronized(m) {
 			writeln("a1-",i);
 		}
 		writeln("a2-",i);
 	}
 }
 void thread2() {
 	foreach (i;0..8) {
 		synchronized(m) {
 			writeln("b1-",i);
 		}
 		writeln("b2-",i);
 	}
 }


 void main() {
 	m = new Mutex();
 	
 	new Thread(&thread1).start;
 	new Thread(&thread2).start;
 }
 ---
 The beginning of the output for this code is:
 a1-0
 a2-0
 a2-0
 b1-0
 b2-0
 b2-0
 a1-1
 a2-1
 a2-1

 Why is the "a2" and "b2" writeln being repeated?!
I don't know, but I can't reproduce either with dmd or ldc. What was your compilation line?
Apr 09 2018
parent reply Jonathan <JonathanILevi gmail.com> writes:
On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
 I don't know, but I can't reproduce either with dmd or ldc. 
 What was your compilation line?
dmd -run file.d
Apr 09 2018
parent reply Jonathan <JonathanILevi gmail.com> writes:
On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:
 On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
 I don't know, but I can't reproduce either with dmd or ldc. 
 What was your compilation line?
dmd -run file.d
I am on Window 10 btw.
Apr 09 2018
next sibling parent Jonathan <JonathanILevi gmail.com> writes:
On Monday, 9 April 2018 at 22:56:33 UTC, Jonathan wrote:
 On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:
 On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
 I don't know, but I can't reproduce either with dmd or ldc. 
 What was your compilation line?
dmd -run file.d
I am on Window 10 btw.
Hum, LDC does not do it for me?
Apr 09 2018
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/9/18 6:56 PM, Jonathan wrote:
 On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:
 On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
 I don't know, but I can't reproduce either with dmd or ldc. What was 
 your compilation line?
dmd -run file.d
I am on Window 10 btw.
It's a windows 32-bit issue (specifically, DMC's FILE *, upon which std.stdio.File is based, is thread unsafe). Try -m64. https://issues.dlang.org/show_bug.cgi?id=18483 http://bugzilla.digitalmars.com/issues/show_bug.cgi?id=327 -Steve
Apr 10 2018
parent Jonathan <JonathanILevi gmail.com> writes:
On Tuesday, 10 April 2018 at 23:59:08 UTC, Steven Schveighoffer 
wrote:
 On 4/9/18 6:56 PM, Jonathan wrote:
 On Monday, 9 April 2018 at 22:53:31 UTC, Jonathan wrote:
 On Monday, 9 April 2018 at 22:49:07 UTC, Cym13 wrote:
 I don't know, but I can't reproduce either with dmd or ldc. 
 What was your compilation line?
dmd -run file.d
I am on Window 10 btw.
It's a windows 32-bit issue (specifically, DMC's FILE *, upon which std.stdio.File is based, is thread unsafe). Try -m64. https://issues.dlang.org/show_bug.cgi?id=18483 http://bugzilla.digitalmars.com/issues/show_bug.cgi?id=327 -Steve
Hum, thank you.
Apr 11 2018