CLR同步块地址 [英] CLR Sync Block Address

查看:287
本文介绍了CLR同步块地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我做的:

public class Employee
{
    public int exp;
}

class Program
{
    static void Main(string[] args)
    {            
        Employee o1 = new Employee();
        o1.exp = 3;
        lock (o1)
        {
            //I am here
        }
    }
}

和O1的获取内存(地址为0x022cf940):

and get memory of o1 (address is 0x022cf940):

我意识到两件事情提到如下:

I realized couple of things mentioned below:


  1. 绿色的长方形同步块,这是12

  2. 蓝色矩形类型的4个字节的地址

  3. 红色矩形是4字节的整数,这是3;

问:哪里是同步数据块的空间,我怎么能找到呢?
什么是12代表什么?

Question: Where is the space of sync blocks and how can I find it? What does "12" stand for?

推荐答案

同步块有多个使用。它可以存储Object.GetHashCode()的值,使得一个对象总是返回时的GetHashCode()再次调用相同的散列码。它可以存储的锁定的语句的所有者线程的ID。它有几个特殊的位,像表明一个对象终结已运行。它可以句柄存储到分配的同步块,在必要的时候一个线程都呼吁的GetHashCode的的使用锁而其中的信息不适合在同步块了。这是非常严重的微优化。

The sync block has more than one use. It can store the value of Object.GetHashCode() so that an object always returns the same hash code when GetHashCode() is called again. It can store the ID of the owner thread of a lock statement. It has several dedicated bits, like indicating that the finalizer for an object has been run. And it can store a handle to an allocated sync block, necessary when a thread both called GetHashCode and used lock and the info can't fit in the sync block anymore. It is very heavily micro-optimized.

您的情况是简单的情况下,只有锁定的叫,没有专用位被打开。所以你看锁的主人,0×12 = 18是拥有锁的线程的Thread.ManagedThreadId。这可以派上相当该死的方便,当你需要解决死锁。

Your case is the simple case, only lock was called and none of the dedicated bits are turned on. So you see the owner of the lock, 0x12 = 18 is the Thread.ManagedThreadId of the thread that owns the lock. That can come in pretty doggone handy when you ever need to troubleshoot a deadlock.

您可以使调试器显示一个有点容易解释时用鼠标右键单击该窗口,选择4字节整数。蓝色矩形是对象(又名型把手)方法表指针。它指示对象的类型,Object.GetType()使用它。红色矩形是对象存储开始其字段。因为你只有在 EXP 字段,它的类型是的Int32,你可以看到三回头。

You can make the debugger display a bit easier to interpret when you right-click the window and select "4-byte Integer". The blue rectangle is the method table pointer for the object (aka "type handle"). It indicates the type of the object, Object.GetType() uses it. The red rectangle is where the object starts storing its fields. Since yours only has the exp field and its type is Int32, you can see 3 back.

这篇关于CLR同步块地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆