Commit 78b25edf authored by Sylvain Thery's avatar Sylvain Thery

resolve MT bug

parent 1b7ac321
......@@ -100,12 +100,15 @@ public:
/// compute thread index in the table of thread
inline unsigned int getCurrentThreadIndex() const;
/// add a thread to the table of thread
inline void addThreadId(const std::thread::id& id);
/// add place for n new threads in the table of thread return index of first
inline unsigned int addEmptyThreadIds(unsigned int n);
/// remove the n last added threads from table
inline void popThreadIds(unsigned int nb);
/// get ref to jth threadId for updating (in thread)
inline std::thread::id& getThreadId(unsigned int j);
protected:
/**
......
......@@ -38,14 +38,21 @@ inline unsigned int GenericMap::getCurrentThreadIndex() const
while (id != m_thread_ids[i])
{
i++;
assert(i<<m_thread_ids.size());
assert(i<m_thread_ids.size());
}
return i;
}
inline void GenericMap::addThreadId(const std::thread::id& id)
//inline void GenericMap::addThreadId(const std::thread::id& id)
//{
// m_thread_ids.push_back(id);
//}
inline unsigned int GenericMap::addEmptyThreadIds(unsigned int n)
{
m_thread_ids.push_back(id);
unsigned int nb = m_thread_ids.size();
m_thread_ids.resize(nb + n);
return nb;
}
inline void GenericMap::popThreadIds(unsigned int nb)
......@@ -55,6 +62,11 @@ inline void GenericMap::popThreadIds(unsigned int nb)
m_thread_ids.pop_back();
}
inline std::thread::id& GenericMap::getThreadId(unsigned int j)
{
return m_thread_ids[j];
}
/****************************************
* BUFFERS MANAGEMENT *
......
......@@ -660,10 +660,10 @@ protected:
bool& m_finished;
unsigned int m_id;
FUNC m_lambda;
std::thread::id& m_threadId; // ref on thread::id in table of threads in genericMap for init at operator()
public:
ThreadFunction(FUNC func, std::vector<CELL>& vd, Utils::Barrier& s1, Utils::Barrier& s2, bool& finished, unsigned int id):
m_cells(vd), m_sync1(s1), m_sync2(s2), m_finished(finished), m_id(id), m_lambda(func)
ThreadFunction(FUNC func, std::vector<CELL>& vd, Utils::Barrier& s1, Utils::Barrier& s2, bool& finished, unsigned int id, std::thread::id& threadId) :
m_cells(vd), m_sync1(s1), m_sync2(s2), m_finished(finished), m_id(id), m_lambda(func), m_threadId(threadId)
{
}
......@@ -672,6 +672,9 @@ public:
void operator()()
{
// first thing to do set the thread id in genericMap
m_threadId = std::this_thread::get_id();
while (!m_finished)
{
for (typename std::vector<CELL>::const_iterator it = m_cells.begin(); it != m_cells.end(); ++it)
......@@ -710,11 +713,12 @@ void foreach_cell_tmpl(MAP& map, FUNC func, unsigned int nbth)
std::thread** threads = new std::thread*[nbth];
ThreadFunction<ORBIT,FUNC>** tfs = new ThreadFunction<ORBIT,FUNC>*[nbth];
// add place for nbth new threads in the table of threadId in genericmap
unsigned int firstThread = map.addEmptyThreadIds(nbth);
for (unsigned int i = 0; i < nbth; ++i)
{
tfs[i] = new ThreadFunction<ORBIT,FUNC>(func, vd[i],sync1,sync2, finished,1+i);
tfs[i] = new ThreadFunction<ORBIT,FUNC>(func, vd[i],sync1,sync2, finished,1+i,map.getThreadId(firstThread+i));
threads[i] = new std::thread( std::ref( *(tfs[i]) ) );
map.addThreadId(threads[i]->get_id());
}
// and continue to traverse the map
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment