hash_trie
hash_trie copied to clipboard
Is this thread safe?
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
- A starts a copy-construction, and manages to copy the data, but has not yet incremented the root.
- B does an insert, and releases the root
- A resumes and increments the root: kBOOM!