How is a critical section associated with each initialized object?

When you speak

lock (obj) ... 

.NET uses the critical section in obj to synchronize the following statements.

How is this critical section initialized? (for example, is it initialized at build time or is it lazy?)

+4
source share
2 answers

Each object receives a 4-byte "block" of allocated memory (syncblk), which is an index in SyncTableEntry. When an object is created, syncblk is set to 0, which prevents additional memory allocation (except for this 4 byte numbers). When the lock is executed, this synchronization is set to the corresponding record in the table, which can then cause distribution. This is essentially lazy initialization.

When you call lock (object), it effectively uses Monitor.Enter for the object, which in turn sets the record correctly. For more information, see the MSDN Article in .NET Internal Languages .

+4
source

According to Microsoft documentation, a process allocates critical section memory when a variable of type CRITICAL_SECTION is declared.

0
source

Source: https://habr.com/ru/post/1387632/


All Articles