I am trying to make my_class
thread-safe like so.
class my_class
{
const std::vector<double>&
get_data() const
{ //lock so that cannot get_data() while setting data
lock l(m_mutex);
return m_data;
}
void
run()
{
vector<double> tmp;
//some calculations on tmp.
{ //lock so that cannot get_data() while setting m_data
lock l(m_mutex);
m_data = tmp; //set the data
}
}
private:
std::vector<double> m_data;
mutex m_mutex;
my_class(); //non-copyable
}
run()
and get_data()
may be called by different openmp
threads and so I introduce a lock.
(Since am using openmp, m_mutex
and lock
are RAII wrappers around omp_init_lock();
etc. commands).
However, the lock on get_data ()
is expensive to create and destroy (The most expensive operation when I profile my code - I call get_data()
a lot).
Is is possible to reorganise my_class to remove the lock in get_data()
? Or is this lock the unavoidable cost of parallelising the code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…