diff options
author | Hieu Hoang <fishandfrolick@gmail.com> | 2012-06-26 21:33:50 +0400 |
---|---|---|
committer | Hieu Hoang <fishandfrolick@gmail.com> | 2012-06-26 21:33:50 +0400 |
commit | 153e80053c4319501323bc15ea66bb668f1ef542 (patch) | |
tree | 0e205bd411b63b8d0adc048c95eb9c53bf089b49 /mert/Vocabulary.h | |
parent | 93bff3f2013b2732c67355d2e9bd253fba4670a7 (diff) |
lock m_vocab variable access in Encode() and Lookup(). Other functions are still not threadsafe
Diffstat (limited to 'mert/Vocabulary.h')
-rw-r--r-- | mert/Vocabulary.h | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/mert/Vocabulary.h b/mert/Vocabulary.h index a8630e951..52c088f18 100644 --- a/mert/Vocabulary.h +++ b/mert/Vocabulary.h @@ -4,6 +4,10 @@ #include <map> #include <string> +#ifdef WITH_THREADS +#include <boost/thread/shared_mutex.hpp> +#endif + namespace mert { /** @@ -21,28 +25,12 @@ class Vocabulary { virtual ~Vocabulary() {} /** Returns the assiged id for given "token". */ - int Encode(const std::string& token) { - iterator it = m_vocab.find(token); - int encoded_token; - if (it == m_vocab.end()) { - // Add an new entry to the vocaburary. - encoded_token = static_cast<int>(m_vocab.size()); - m_vocab[token] = encoded_token; - } else { - encoded_token = it->second; - } - return encoded_token; - } + int Encode(const std::string& token); /** * Return true iff the specified "str" is found in the container. */ - bool Lookup(const std::string&str , int* v) const { - const_iterator it = m_vocab.find(str); - if (it == m_vocab.end()) return false; - *v = it->second; - return true; - } + bool Lookup(const std::string&str , int* v) const; void clear() { m_vocab.clear(); } @@ -62,6 +50,12 @@ class Vocabulary { private: std::map<std::string, int> m_vocab; + +#ifdef WITH_THREADS + //reader-writer lock + mutable boost::shared_mutex m_accessLock; +#endif + }; class VocabularyFactory { |