1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-16 13:10:35 +00:00
gnss-sdr/src/core/receiver/concurrent_map.h

103 lines
2.8 KiB
C
Raw Normal View History

/*!
* \file concurrent_map.h
* \brief Interface of a thread-safe std::map
* \author Javier Arribas, 2011. jarribas(at)cttc.es
*
* -------------------------------------------------------------------------
*
* Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors)
*
* GNSS-SDR is a software defined Global Navigation
* Satellite Systems receiver
*
* This file is part of GNSS-SDR.
*
* GNSS-SDR is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
2014-05-07 21:35:44 +00:00
* (at your option) any later version.
*
* GNSS-SDR is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNSS-SDR. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
#ifndef GNSS_SDR_CONCURRENT_MAP_H
#define GNSS_SDR_CONCURRENT_MAP_H
#include <boost/thread/mutex.hpp>
#include <map>
#include <utility>
template <typename Data>
/*!
* \brief This class implements a thread-safe std::map
*
*/
class Concurrent_Map
{
typedef typename std::map<int, Data>::iterator Data_iterator; // iterator is scope dependent
private:
std::map<int, Data> the_map;
boost::mutex the_mutex;
public:
void write(int key, Data const& data)
{
boost::mutex::scoped_lock lock(the_mutex);
Data_iterator data_iter;
data_iter = the_map.find(key);
if (data_iter != the_map.end())
2014-05-07 21:35:44 +00:00
{
data_iter->second = data; // update
2014-05-07 21:35:44 +00:00
}
else
{
the_map.insert(std::pair<int, Data>(key, data)); // insert SILENTLY fails if the item already exists in the map!
2014-05-07 21:35:44 +00:00
}
lock.unlock();
}
std::map<int, Data> get_map_copy()
{
boost::mutex::scoped_lock lock(the_mutex);
std::map<int, Data> map_aux = the_map;
lock.unlock();
2015-05-12 21:45:56 +00:00
return map_aux;
}
2015-05-12 21:45:56 +00:00
size_t size()
{
boost::mutex::scoped_lock lock(the_mutex);
2015-05-12 21:45:56 +00:00
size_t size_ = the_map.size();
lock.unlock();
2015-05-12 21:45:56 +00:00
return size_;
}
bool read(int key, Data& p_data)
{
boost::mutex::scoped_lock lock(the_mutex);
Data_iterator data_iter;
data_iter = the_map.find(key);
if (data_iter != the_map.end())
{
p_data = data_iter->second;
lock.unlock();
return true;
}
lock.unlock();
return false;
}
};
#endif