Imagine that we are developing some online services. We have an unordered map of registered users with some properties for each user. This set is accessed by many threads, but it is very rarely modified. All operations with the following set are done in a thread-safe manner:
#include <unordered_map>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
struct user_info {
std::string address;
unsigned short age;
// Other parameters
// ...
};
class users_online {
typedef boost::mutex mutex_t;
mutable mutex_t users_mutex_;
std::unordered_map<std::string, user_info> users_;
public:
bool is_online(const std::string& username) const {
boost::lock_guard<mutex_t> lock(users_mutex_);
return users_.find(username) != users_.end();
}
std...