philsquared / hash_trie

A persistent hash array-mapped trie for C++
BSD 2-Clause "Simplified" License
90 stars 9 forks source link

Is this thread safe? #5

Open aaron-michaux opened 1 year ago

aaron-michaux commented 1 year ago

We have:

 template<typename T>
    struct hash_trie_data {
        branch_node<T> const* m_root;
        size_t m_size;
    };

Then in the copy constructor, we copy as follows:

       explicit hash_trie( hash_trie_data<T> const& data ) : m_data( data ) {
            addref( m_data.m_root );
        }
        hash_trie( hash_trie<T> const& other ) : hash_trie( other.m_data ) {}

But, for example, insert:


        template<typename U>
        auto insert( U &&value ) {
            if( auto newRoot = inserted( m_data.m_root, std::forward<U>(value) ) ) {
                release( m_data.m_root );
                m_data = { newRoot, size()+1 };
            }
        }

So... insert would race with copy construction on another thread. Give threads A and B