简体   繁体   English

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

[英]What's the best way to determine if NamedDataSlot exists

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() ...) 这里显而易见的问题是:如果某些代码调用DoesNamedDataSlotExist()两次,它将首先生成false然后为true (如果我使用Thread.FreeNamedDataSlot()可以优化它...)

But is there any better way? 但还有更好的方法吗?

EDIT 编辑

source of GetNamedDataSlot 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 ... 不知怎的,我需要访问this.m_KeyToSlotMap ...

You can duplicate the behavior you observe in GetNamedDataSlot source. 您可以复制在GetNamedDataSlot源中观察到的行为。

You can introduce special entity, say Thread local storage adapter, that will maintain the dictionary of already allocated data slots. 您可以引入特殊实体,比如Thread本地存储适配器,它将维护已分配数据槽的字典。 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;            
        }       
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查 S3 object 是否存在的最佳方法是什么? - what's best way to check if a S3 object exists? 确定的最佳方法是双倍不是零 - what is the best way to determine is a double is not zero 在c#中确定程序员是通过IDE还是用户运行程序的最佳方法是什么? - What is the best way in c# to determine whether the programmer is running the program via IDE or it's user? C# TcpClient - 客户端确定远程服务器是否正常关闭连接的最佳方法是什么? - C# TcpClient - What's the best way for a client to determine if remote server has gracefully shutdown connection? 确定应用程序根目录的最佳方法是什么? - What is the best way to determine application root directory? 检查网站文件夹是否存在的最佳方法是什么? - What is the best way to check if a website folder exists? 什么是多线程的最佳方式? - What's the best way to multithread? 确定应用程序位置的正确方法是什么? - What is the proper way to determine an application's location? 使用Linq to SQL确定行是否存在的最快方法是什么? - What is the fastest way to determine if a row exists using Linq to SQL? 确定2个URL是否相同的最安全的方法是什么? - What's the safest way to determine if 2 URLs are the same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM