Skip to content

Thread-unsafe code #8

Description

@gggrafff

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];
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions