mirror of
https://github.com/copyrighttxt/watrbx-game-engine.git
synced 2026-09-04 20:57:49 +00:00
43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
namespace util
|
|
{
|
|
template <typename F>
|
|
struct ConcurrentLoops
|
|
{
|
|
static int run(F f, int iterations, int num_threads)
|
|
{
|
|
int result = 0;
|
|
std::vector<std::pair<boost::thread*, int> > threads(num_threads);
|
|
int index = 0;
|
|
for (auto &it : threads)
|
|
{
|
|
it.first = new boost::thread(threadFunc, f, index++, iterations, it.second);
|
|
}
|
|
|
|
for (auto &it : threads)
|
|
{
|
|
it.first->join();
|
|
delete it.first;
|
|
it.first = 0;
|
|
if (it.second)
|
|
result = it.second;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
static void threadFunc(F &f, int index, int iterations, int &result)
|
|
{
|
|
for (int i = 0; i < iterations; ++i)
|
|
{
|
|
result = f(index);
|
|
if (result)
|
|
break;
|
|
}
|
|
}
|
|
}; // struct ConcurrentLoops
|
|
|
|
} // namespace utils
|