private: using Entry = std::pair<std::string, int>; using EntryList = std::vector<Entry>; using Map = std::map<std::string, EntryList>; using MapPtr = std::shared_ptr<Map>;
intCustomData::query(const std::string& customer, const std::string& stock)const{ MapPtr data = getData(); // Once the data is gotten, the lock isn't needed anymore auto entries = data->find(customer); if (entries == data->end()) { return-1; } returnfindEntry(entries->second, stock); }
voidCustomData::update(const std::string& customer, const EntryList& entries){ std::lock_guard<std::mutex> lock(m_mutex); // must leverage the lock in the whole time if (!m_data.unique()) { // distinguish if it's been reading MapPtr newData = std::make_shared<Map>(*m_data); m_data.swap(newData); } assert(m_data.unique()); (*m_data)[customer] = entries; }