简体   繁体   English

StringHashTable中使用的静态函数线程安全?

[英]an static function used in StringHashTable thread-safe?

I have got an StringHashTable class from 我有一个StringHashTable类
http://preshing.com/20110603/hash-table-performance-tests/ http://preshing.com/20110603/hash-table-performance-tests/

The following are parts of source : 以下是部分来源:

class StringHashTable
{
    static uint fnv1Hash(const char *key)
    {
        unsigned int hash = 2166136261ul;
        for (const char *s = key; *s; s++)
            hash = (16777619 * hash) ^ (*s);
        return hash;
    };

    uint &operator[](const char *key)
    {
        uint hash = fnv1Hash(key) & (m_tableSize - 1);
        Bucket *firstBucket = m_table + hash;
        Bucket *b = firstBucket;
        if (b->key)
        {
            do
            {
                if (strcmp(b->key, key) == 0)
                    return b->value;// Found existing bucket
                b = b->next;
            } while (b);
        }
        ..........
    }
} 

Suppose that I have the global var : 假设我有全局变量:

StringHashTable hashtable(1024) ; //m_tableSize now 1024

And then the following is in main : 然后主要是以下内容:

hashtable["0000"] = 0 ;
....
hashtable["9999"] = 9999 ;

After fill in all the data I need , thread 1 to n will get value according to key 填写完我需要的所有数据后,线程1至n将根据键获取值

while(1)
{
    s = get(); //return string like "0000" ... "9999"
    echo << hashtable[s.c_str()] << endl ;
}

I wonder if the StringHashTable would work fine in thread at first , because the function fnv1Hash is static , on second thought , there is no static member data in this StringHashTable , so while thread1 is doing hashtable["0000"] and thread2 is doing hashtable["9999"] at the very same time both thread1 are calling fnv1Hash they will both get the right hash returned !!! 我不知道StringHashTable首先是否可以在线程中正常工作,因为函数fnv1Hash是静态的,再考虑一下,此StringHashTable中没有静态成员数据,因此当thread1在执行hashtable [“ 0000”]而thread2在执行hashtable时[“ 9999”]同时两个thread1都调用fnv1Hash,它们都将返回正确的哈希!

My question is : different thread call static uint fnv1Hash(const char *key) with different key at the very same time still work fine ? 我的问题是:同时使用不同密钥的不同线程调用静态uint fnv1Hash(const char * key)仍然可以正常工作吗? In StringHashTable , fnv1Hash is static for any reason ?! 在StringHashTable中,由于任何原因fnv1Hash是静态的?

The function fnv1Hash() doesn't access any non-local state other than the data pointed to by key . 函数fnv1Hash()除了key指向的数据之外,不访问任何非本地状态。 Assuming the content of the array key points to isn't written to concurrently, there is no threading issue. 假设数组key指向的内容不是同时写入的,则不存在线程问题。 Of course, if another thread writes to the array pointed to by key , all bets are off. 当然,如果另一个线程写入key指向的数组,则所有选择都将关闭。

Given that fnv1Hash() does access any of the object's data it doesn't need a this pointer. 鉴于fnv1Hash()确实访问了对象的任何数据,因此不需要this指针。 Thus, it is made static to indicate both to the human reader and the compiler that the objects won't be accessed implicitly. 因此,将其static地向人类读者和编译器指示不会隐式访问对象。 For the compiler the upshot is that it doesn't need to pass a this pointer. 对于编译器,结果是它不需要传递this指针。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM