According to TDPL, multiple-arguments synchronized locks them in increasing address order so as to prevent deadlock. What the statement actually does is... use the comma expression for the arguments, so only the last object gets locked.
Comment #1 by andrei — 2016-05-22T12:48:13Z
Test code (thanks Steve Schveighoffer) should not print "oops!" but does:
import std.concurrency;
import core.sync.mutex;
import core.thread;
import std.stdio;
__gshared Mutex a;
__gshared Mutex b;
shared static this()
{
a = new Mutex;
b = new Mutex;
}
void badThread()
{
synchronized(a)
{
writeln("a is locked!");
while(1) { Thread.sleep(1.seconds); }
}
}
void main()
{
spawn(&badThread);
Thread.sleep(1.seconds);
synchronized(a, b)
{
writeln("oops!");
}
}
Comment #2 by yebblies — 2018-01-24T15:06:11Z
*** Issue 18277 has been marked as a duplicate of this issue. ***
Comment #3 by maxhaton — 2020-09-24T21:14:19Z
Since around dmd ~2.078, one cannot use the comma operator at all so this particular construct is no longer an issue
See https://run.dlang.io/is/EL9foO
Comment #4 by nick — 2023-12-04T11:14:35Z
Reopening as comment #1 shows this issue is for multiple arguments to be supported.
Comment #5 by robert.schadek — 2024-12-13T18:47:56Z