I am trying to multithread multiple function calls using this function:
template <typename Range, typename Func>
void parallel_for_each(ThreadPool& tp, Range& range, Func func) {
if (range.size() > 1) {
auto len = range.size();
auto num_threads = std::min((size_t)THREAD_AMOUNT, len);
auto step = len / num_threads;
for (size_t i = 0; i < len; i += step)
tp.enqueue([i, step, func, &range] {
auto curr = std::next(std::begin(range), i);
auto end = std::next(curr, step);
for (; curr != end; ++curr)
func(*curr);
});
}
}
That I am using like so:
//Check tank collision and nudge tanks away from each other
{
std::lock_guard<std::mutex> lock(tanks_mutex);
parallel_for_each(*thread_pool, tanks, [this](Tank& tank) { return check_collision(tank); });
}
//Update tanks
{
std::lock_guard<std::mutex> lock(tanks_mutex);
parallel_for_each(*thread_pool, tanks, [this](Tank& tank) { return update_tank(tank); });
}
//Update smoke plumes
{
std::lock_guard<std::mutex> lock(smokes_mutex);
parallel_for_each(*thread_pool, smokes, [this](Smoke& smoke) { return update_smoke_plume(smoke); });
}
etc...
But I get:
Exception thrown at 0x0000000140001A2F in Tmpl8_2019-01_debug.exe: 0xE0736171: Access violation reading location 0xFFFFFFFFFFFFFFFF.
Exception thrown at 0x00007FFB085F051C (KernelBase.dll) in Tmpl8_2019-01_debug.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.