Strange Thread Causing Duplicating `writeln`

Jonathan JonathanILevi at gmail.com
Mon Apr 9 22:28:45 UTC 2018


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?!


More information about the Digitalmars-d-learn mailing list