diff options
author | Nicola Bertoldi <bertoldi@fbk.eu> | 2015-05-04 10:24:36 +0300 |
---|---|---|
committer | Nicola Bertoldi <bertoldi@fbk.eu> | 2015-05-04 10:24:36 +0300 |
commit | 85a4e48df4ba08eabdde5097838de18efcc31aea (patch) | |
tree | fef99c50b0e3afb15d9b25bd1f5efa29a8327f55 | |
parent | 3400b622c0ff11ad690ef7933b062ec53789a13f (diff) |
made use of boost::shared_mutex dependent from WITH_THREADS
-rw-r--r-- | moses/ContextScope.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/moses/ContextScope.h b/moses/ContextScope.h index ed9f854ff..c01489848 100644 --- a/moses/ContextScope.h +++ b/moses/ContextScope.h @@ -26,7 +26,9 @@ namespace Moses typedef scratchpad_t::value_type entry_t; typedef scratchpad_t::const_iterator const_iter_t; scratchpad_t m_scratchpad; +#ifdef WITH_THREADS mutable boost::shared_mutex m_lock; +#endif public: // class write_access // { @@ -52,7 +54,9 @@ namespace Moses boost::shared_ptr<void> const& set(void const* const key, boost::shared_ptr<T> const& val) { +#ifdef WITH_THREADS boost::unique_lock<boost::shared_mutex> lock(m_lock); +#endif return (m_scratchpad[key] = val); } @@ -60,24 +64,30 @@ namespace Moses boost::shared_ptr<T> const get(void const* key, bool CreateNewIfNecessary=false) { +#ifdef WITH_THREADS using boost::shared_mutex; using boost::upgrade_lock; // T const* key = reinterpret_cast<T const*>(xkey); upgrade_lock<shared_mutex> lock(m_lock); + #endif iter_t m = m_scratchpad.find(key); boost::shared_ptr< T > ret; if (m != m_scratchpad.end()) { if (m->second == NULL && CreateNewIfNecessary) { +#ifdef WITH_THREADS boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); +#endif m->second.reset(new T); } ret = boost::static_pointer_cast< T >(m->second); return ret; } if (!CreateNewIfNecessary) return ret; +#ifdef WITH_THREADS boost::upgrade_to_unique_lock<shared_mutex> xlock(lock); +#endif ret.reset(new T); m_scratchpad[key] = ret; return ret; @@ -87,8 +97,10 @@ namespace Moses ContextScope(ContextScope const& other) { +#ifdef WITH_THREADS boost::unique_lock<boost::shared_mutex> lock1(this->m_lock); boost::unique_lock<boost::shared_mutex> lock2(other.m_lock); +#endif m_scratchpad = other.m_scratchpad; } |