确定 NamedDataSlot 是否存在的最佳方法是什么

发布于 2024-10-20 19:07:54 字数 1098 浏览 4 评论 0原文

实际上我想出了以下实现

bool DoesNamedDataSlotsExist(string name)
{
    try
    {
        Thread.AllocateNamedDataSlot(name);
    }
    catch
    {
        return true;
    }
    return false;
}

这里明显的问题:如果某些代码调用 DoesNamedDataSlotExist() 两次,它将首先生成 false 然后 true (如果我使用 Thread.FreeNamedDataSlot() 可以优化...)

但是有更好的方法吗?

编辑

GetNamedDataSlot 的来源

public LocalDataStoreSlot GetNamedDataSlot(string name)
{
    LocalDataStoreSlot slot2;
    bool tookLock = false;
    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
        Monitor.ReliableEnter(this, ref tookLock);
        LocalDataStoreSlot slot = (LocalDataStoreSlot) this.m_KeyToSlotMap[name];
        if (slot == null)
        {
            return this.AllocateNamedDataSlot(name);
        }
        slot2 = slot;
    }
    finally
    {
        if (tookLock)
        {
            Monitor.Exit(this);
        }
    }
    return slot2;
}

不知何故,我需要访问 this.m_KeyToSlotMap...

Actually I come up with the following implementation

bool DoesNamedDataSlotsExist(string name)
{
    try
    {
        Thread.AllocateNamedDataSlot(name);
    }
    catch
    {
        return true;
    }
    return false;
}

The obvious problem here: If some code calls DoesNamedDataSlotExist() twice, it will first generate false then true (which could be optimized if i would use Thread.FreeNamedDataSlot() ...)

But is there any better way?

EDIT

source of GetNamedDataSlot

public LocalDataStoreSlot GetNamedDataSlot(string name)
{
    LocalDataStoreSlot slot2;
    bool tookLock = false;
    RuntimeHelpers.PrepareConstrainedRegions();
    try
    {
        Monitor.ReliableEnter(this, ref tookLock);
        LocalDataStoreSlot slot = (LocalDataStoreSlot) this.m_KeyToSlotMap[name];
        if (slot == null)
        {
            return this.AllocateNamedDataSlot(name);
        }
        slot2 = slot;
    }
    finally
    {
        if (tookLock)
        {
            Monitor.Exit(this);
        }
    }
    return slot2;
}

Somehow I would need to access this.m_KeyToSlotMap...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2024-10-27 19:07:55

您可以复制在 GetNamedDataSlot 源中观察到的行为。

您可以引入特殊实体,例如线程本地存储适配器,它将维护已分配的数据槽的字典。所有数据时隙的分配都应通过该实体进行。

这就是我的意思

internal static class TLSAdapter
{
    static Dictionary<string, LocalDataStoreSlot> tlsSlots = new Dictionary<string, LocalDataStoreSlot>();

    public static bool DoesNamedDataSlotsExist(string name)
    {
        lock(tlsSlots)
        {
            return tlsSlots.ContainsKey(name);
        }

    }

    public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
    {
        lock(tlsSlots)
        {
            LocalDataStoreSlot slot = null;
            if ( tlsSlots.TryGetValue(name, out slot) )
                return slot;

            slot = Thread.GetNamedDataSlot(name);
            tlsSlots[name] = slot;
            return slot;            
        }       
    }   
}

You can duplicate the behavior you observe in GetNamedDataSlot source.

You can introduce special entity, say Thread local storage adapter, that will maintain the dictionary of already allocated data slots. All allocations of data slots should be made via this entity.

Here's what I mean

internal static class TLSAdapter
{
    static Dictionary<string, LocalDataStoreSlot> tlsSlots = new Dictionary<string, LocalDataStoreSlot>();

    public static bool DoesNamedDataSlotsExist(string name)
    {
        lock(tlsSlots)
        {
            return tlsSlots.ContainsKey(name);
        }

    }

    public static LocalDataStoreSlot AllocateNamedDataSlot (string name)
    {
        lock(tlsSlots)
        {
            LocalDataStoreSlot slot = null;
            if ( tlsSlots.TryGetValue(name, out slot) )
                return slot;

            slot = Thread.GetNamedDataSlot(name);
            tlsSlots[name] = slot;
            return slot;            
        }       
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文