Thread overview
Strange Thread Causing Duplicating `writeln`
Apr 09, 2018
Jonathan
Apr 09, 2018
Cym13
Apr 09, 2018
Jonathan
Apr 09, 2018
Jonathan
Apr 10, 2018
Jonathan
Apr 11, 2018
Jonathan
April 09, 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?!
April 09, 2018
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?
April 09, 2018
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
April 09, 2018
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.
April 10, 2018
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?
April 10, 2018
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
April 11, 2018
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.