Do we even need a lock on accessing the data? We're only reading from it here. We definitely need a lock on the counter and the thing we're writing to, but the input is shared across all function calls.
|
void worker_thread() { |
|
|
|
while(!m_done) |
|
{ |
|
if(m_has_an_input){ |
|
|
|
// call the expensive function |
|
std::shared_lock<std::shared_mutex> param_lock(m_param_mut); |
|
func_output_t val = m_f(m_param, m_observed_data); |
|
|
|
// write it to the average and increment count |
|
// do this in thread-safe way with mutex |
|
std::lock_guard<std::mutex> out_lock{m_ac_mut}; |
|
if( m_count.load() < m_total_calcs ) { |
|
m_working_ave += val / m_total_calcs; |
|
m_count++; |
|
}else if( m_count.load() == m_total_calcs){ |
|
m_out.set_value(m_working_ave); |
|
m_has_an_input = false; |
|
m_count++; |
|
} |
|
|
|
}else{ |
|
std::this_thread::yield(); |
|
} |
|
} |
|
} |
Do we even need a lock on accessing the data? We're only reading from it here. We definitely need a lock on the counter and the thing we're writing to, but the input is shared across all function calls.
ssme/include/ssme/thread_pool.h
Lines 103 to 129 in a535e90