Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is this thread safe? #5

Open
aaron-michaux opened this issue Dec 1, 2022 · 0 comments
Open

Is this thread safe? #5

aaron-michaux opened this issue Dec 1, 2022 · 0 comments

Comments

@aaron-michaux
Copy link

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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant