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.cpp | |
parent | 93bff3f2013b2732c67355d2e9bd253fba4670a7 (diff) |
lock m_vocab variable access in Encode() and Lookup(). Other functions are still not threadsafe
Diffstat (limited to 'mert/Vocabulary.cpp')
-rw-r--r-- | mert/Vocabulary.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/mert/Vocabulary.cpp b/mert/Vocabulary.cpp index 40b04bf99..744917d21 100644 --- a/mert/Vocabulary.cpp +++ b/mert/Vocabulary.cpp @@ -6,6 +6,42 @@ namespace { Vocabulary* g_vocab = NULL; } // namespace +int Vocabulary::Encode(const std::string& token) { +#ifdef WITH_THREADS + boost::upgrade_lock<boost::shared_mutex> read_lock(m_accessLock); +#endif // WITH_THREADS + + 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()); + +#ifdef WITH_THREADS + boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(read_lock); +#endif // WITH_THREADS + + m_vocab[token] = encoded_token; + } else { +#ifdef WITH_THREADS + boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(read_lock); +#endif // WITH_THREADS + encoded_token = it->second; + } + return encoded_token; +} + +bool Vocabulary::Lookup(const std::string&str , int* v) const { +#ifdef WITH_THREADS + boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock); +#endif // WITH_THREADS + + const_iterator it = m_vocab.find(str); + if (it == m_vocab.end()) return false; + *v = it->second; + return true; +} + Vocabulary* VocabularyFactory::GetVocabulary() { if (g_vocab == NULL) { return Singleton<Vocabulary>::GetInstance(); |