On Tuesday, 22 March 2016 at 10:49:01 UTC, Johan Engelen wrote:
Quiz: does this compile or not?
```
class Klass {}
void main() {
immutable Klass klass = new Klass;
synchronized (klass)
{
// do smth
}
}
```
A D object contains two (!) hidden pointers. Two? Yes: the
vtable pointer __vptr, and a pointer to a Monitor struct which
contains a synchronization mutex.
The synchronized statement is lowered into druntime calls that
*write* to __monitor.
Quiz answer: yes it compiles. Oops?
This is related to an earlier discussion on whether TypeInfo
objects should be immutable or not [1]. Should one be able to
synchronize on typeid(...) or not?
```
interface Foo {}
void main() {
synchronized(typeid(Foo)) {
// do smth
}
}
```
Because LDC treats the result of typeid as immutable, the code
is bugged depending on the optimization level.
[1]
http://forum.dlang.org/post/[email protected]
As long as there's no race conditions in the initial creation of
the mutex, it shouldn't matter, even though it does internally
mutate the object, because it's transparent to developers (unless
you're going out of your way to access the internal __monitor
field).
What exactly is bugged about the typeid example under LDC?