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

Thread-unsafe code #8

Open
gggrafff opened this issue Jul 13, 2020 · 0 comments · May be fixed by #10
Open

Thread-unsafe code #8

gggrafff opened this issue Jul 13, 2020 · 0 comments · May be fixed by #10

Comments

@gggrafff
Copy link

static std::map<TransferImpl*, std::weak_ptr<TransferImpl>> m_TransferMap;

This field is static. Access to change it is shared by all objects of the class. Creating multiple objects in different threads results in a data race.

Please use a mutex and encapsulate operations with this field.

Example:

using TransferStorage = std::map<TransferImpl*, std::weak_ptr<TransferImpl>>;
static TransferStorage m_TransferMap;
static std::mutex m_TransferMap_mutex;

template<class T>
static std::pair<TransferStorage::iterator, bool> insert_transfer(T&& value) {
  std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
  return m_TransferMap.insert(std::move(value));
}

static size_t erase_transfer(TransferImpl* index) {
  std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
  return m_TransferMap.erase(index);
}

static std::weak_ptr<TransferImpl> get_transfer(TransferImpl* index) {
  std::lock_guard<std::mutex> lk(m_TransferMap_mutex);
  return m_TransferMap[index];
}
@gggrafff gggrafff linked a pull request Oct 1, 2020 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant