Fix locking in AlignmentInfoCollection.*

This commit is contained in:
junczys 2012-09-11 16:06:05 +02:00
parent b317522563
commit bac859c451
5 changed files with 22 additions and 4 deletions

View File

@ -1 +0,0 @@
/usr/share/automake-1.9/INSTALL

View File

@ -1 +0,0 @@
/usr/share/automake-1.10/COPYING

View File

@ -1 +0,0 @@
/usr/share/automake-1.10/INSTALL

View File

@ -38,8 +38,18 @@ const AlignmentInfo &AlignmentInfoCollection::GetEmptyAlignmentInfo() const
const AlignmentInfo *AlignmentInfoCollection::Add(
const std::set<std::pair<size_t,size_t> > &pairs)
{
AlignmentInfo pairsAlignmentInfo(pairs);
#ifdef WITH_THREADS
{
boost::shared_lock<boost::shared_mutex> read_lock(m_accessLock);
AlignmentInfoSet::const_iterator i = m_collection.find(pairsAlignmentInfo);
if (i != m_collection.end())
return &*i;
}
boost::unique_lock<boost::shared_mutex> lock(m_accessLock);
#endif
std::pair<AlignmentInfoSet::iterator, bool> ret =
m_collection.insert(AlignmentInfo(pairs));
m_collection.insert(pairsAlignmentInfo);
return &(*ret.first);
}

View File

@ -23,6 +23,11 @@
#include <set>
#ifdef WITH_THREADS
#include <boost/thread/shared_mutex.hpp>
#include <boost/thread/locks.hpp>
#endif
namespace Moses
{
@ -52,6 +57,12 @@ class AlignmentInfoCollection
AlignmentInfoCollection();
static AlignmentInfoCollection s_instance;
#ifdef WITH_THREADS
//reader-writer lock
mutable boost::shared_mutex m_accessLock;
#endif
AlignmentInfoSet m_collection;
const AlignmentInfo *m_emptyAlignmentInfo;
};