3 #include <boost/asio.hpp> 4 #include <boost/thread.hpp> 5 #include <boost/function.hpp> 6 #include <noise/RandomnessGenerator.h> 7 #include <UniformNoiseSource.h> 28 boost::asio::io_service::work
work_;
50 boost::condition_variable
cond_;
89 explicit thread_pool(std::size_t _pool_size,
double deviceAccuracy)
91 pool_size(_pool_size),
92 available_(_pool_size)
94 apMatrices =
new std::vector<std::vector<double>>[
pool_size];
97 intersectionHandlingNoiseSources =
99 resourceSetAvailable =
new bool[
pool_size];
102 for (std::size_t i = 0; i <
pool_size; ++i)
104 resourceSetAvailable[i] =
true;
105 randGens[i] = *DEFAULT_RG;
108 randGens2[i] = *DEFAULT_RG;
111 *DEFAULT_RG, 0.0, 1.0
113 threads_.create_thread(boost::bind(
114 &boost::asio::io_service::run,
129 catch (
const std::exception&) {}
153 if(resourceSetAvailable[i])
return i;
171 template <
typename Task >
void run_task(Task task){
172 boost::unique_lock< boost::mutex > lock(mutex_);
175 if (0 == available_){
184 resourceSetAvailable[resourceIdx] =
false;
193 std::vector<std::vector<double>>&,
208 boost::unique_lock<boost::mutex> lock(mutex_);
209 while(available_ < pool_size){
224 boost::function<
void(
225 std::vector<std::vector<double>>&,
236 apMatrices[resourceIdx],
237 randGens[resourceIdx],
238 randGens2[resourceIdx],
239 intersectionHandlingNoiseSources[resourceIdx]
243 catch (
const std::exception &e) {
244 std::stringstream ss;
245 ss <<
"thread_pool::wrap_task EXCEPTION: " << e.what();
246 logging::WARN(ss.str());
250 boost::unique_lock< boost::mutex > lock(mutex_);
252 resourceSetAvailable[resourceIdx] =
true;
std::size_t getPoolSize()
Obtain the thread pool size.
Definition: ThreadPool.h:165
RandomnessGenerator< double > * randGens2
Second randomness generators (to substitute old box muller), one per thread.
Definition: ThreadPool.h:66
void wrap_task(boost::function< void(std::vector< std::vector< double >> &, RandomnessGenerator< double > &, RandomnessGenerator< double > &, NoiseSource< double > &)> &task, int resourceIdx)
Wrap a task so that available threads count can be increased once provided task has been completed...
Definition: ThreadPool.h:223
boost::condition_variable cond_
Condition var to handle tasks dispatching depending on available threads.
Definition: ThreadPool.h:50
boost::mutex mutex_
Mutex to handle concurrent tasks.
Definition: ThreadPool.h:45
Class representing a thread pool to deal with multi threading tasks.
Definition: ThreadPool.h:14
void join()
Lock until all pending threads have finished.
Definition: ThreadPool.h:207
UniformNoiseSource< double > * intersectionHandlingNoiseSources
Intersection handling noise sources, one per thread.
Definition: ThreadPool.h:70
boost::asio::io_service io_service_
Instance of boost input/output service for asynchronous data processing.
Definition: ThreadPool.h:22
int getAvailableResourceSetIndex()
Obtain the index of an available resource set.
Definition: ThreadPool.h:151
RandomnessGenerator< double > * randGens
First randomness generators (general purpose), one per thread.
Definition: ThreadPool.h:61
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:32
std::size_t available_
Number of available threads, those which are not currently performing a task.
Definition: ThreadPool.h:41
bool * resourceSetAvailable
Array of flags specifying availability of resource sets.
Definition: ThreadPool.h:78
void run_task(Task task)
Run a task when there is an available thread for it.
Definition: ThreadPool.h:171
boost::thread_group threads_
Group of threads.
Definition: ThreadPool.h:36
boost::asio::io_service::work work_
Instance of work to report the io service when it has pending tasks.
Definition: ThreadPool.h:28
void computeNormalDistribution(RealType mean, RealType stdev)
Compute a normal distribution using the specified real data type.
Definition: RandomnessGenerator.h:380
std::vector< std::vector< double > > * apMatrices
Definition: ThreadPool.h:56
void computeUniformRealDistribution(RealType lowerBound, RealType upperBound)
Compute a uniform real distribution using the specified real data type.
Definition: RandomnessGenerator.h:343
thread_pool(std::size_t _pool_size, double deviceAccuracy)
Thread pool constructor.
Definition: ThreadPool.h:89