This is a simple extension of the classic dead end to the case when one of the locks is provided by the compiler.
void A2B() { a.Lock(); B(); a.Unlock(); } void B() { b.Lock(); ...; b.Unlock(); } void B2A() { b.Lock(); A(); b.Unlock(); } void A() { a.Lock(); ...; a.Unlock(); }
A classic deadlock occurs if one thread calls A2B()
and another thread calls B2A()
.
In a static initialization lock, the compiler provides a lock b
.
int A() { a.Lock(); ...; a.Unlock(); return 0; } void B2A() { static int v = A(); } void A2B() { a.Lock(); B2A(); a.Unlock(); }
If you assume a static initialization lock, then the code is secretly converted to
void B2A() { if (!initialized) { b.Lock();
One thread calls A2B()
, and the rest calls B2A()
.
source share