Attempt to fix crashes realted to multithreading and std::string

Currently t_string isn't threadsafe, of one thread executes a 't_string
a("a")' and another threads does 't_string b("b")' the progamm might
crash even of those object are completeley unrelated at first glance.

This commit tried to fix this by wrapping the acces to the shared
hash_map object in mutex locks, another attempt to fix this problem
might be to remove the shared hash_map implementation of t_string
completeley.  Unfortunateley i don't know why it was implemented this
way in the first place so i don't know which disadvantages removing it
would have.
This commit is contained in:
gfgtdf 2016-04-23 15:39:21 +02:00
parent b681fa576a
commit 1cfa62ff56

View File

@ -22,6 +22,7 @@
#include <map>
#include <vector>
#include <mutex>
#include "tstring.hpp"
#include "gettext.hpp"
@ -528,21 +529,24 @@ typedef boost::multi_index_container<
> hash_map;
typedef typename hash_map::template nth_index<0>::type hash_index;
};
static types<t_string_base>::hash_map& map() { static types<t_string_base>::hash_map* map = new types<t_string_base>::hash_map; return *map; }
static types<t_string_base>::hash_index& index() { return map().get<0>(); }
static std::mutex& mutex() { static std::mutex* m = new std::mutex(); return *m; }
typedef shared_node<t_string_base> node;
template<>
const node* shared_object<t_string_base>::insert_into_index(const node & n)
{
std::lock_guard<std::mutex> lock(mutex());
return &*index().insert(n).first;
}
template<>
void shared_object<t_string_base>::erase_from_index(const node * ptr)
{
std::lock_guard<std::mutex> lock(mutex());
index().erase(index().find(ptr->val));
}