110#ifndef VSPLINE_MULTITHREAD_H
111#define VSPLINE_MULTITHREAD_H
115#ifndef VSPLINE_SINGLETHREAD
123#include <condition_variable>
138#ifdef VSPLINE_SINGLETHREAD
147template <
bool dummy = true >
148int multithread ( std::function <
void() > payload ,
149 std::size_t nr_workers = 1 )
153 if ( nr_workers <= 0 )
167template <
typename T >
170 typedef T value_type ;
174 atomic (
const value_type & _value )
175 : value ( _value ) { } ;
182 value_type operator++()
187 value_type operator--()
192 value_type operator++ (
int )
197 value_type operator-- (
int )
202 value_type fetch_sub ( value_type arg )
204 value_type v = value ;
209 value_type fetch_add ( value_type arg )
211 value_type v = value ;
224template <
typename T >
using atomic = std::atomic < T > ;
263template <
typename index_t >
267 index_t _index = --source ;
283template <
typename index_t >
285 const index_t & total ,
288 index_t _index = --source ;
293 index = total - 1 - _index ;
310template <
typename index_t >
312 const index_t & count ,
316 index_t high_index = source.fetch_sub ( count ) ;
317 index_t low_index = high_index - count ;
319 if ( high_index <= 0 )
335template <
typename index_t >
337 const index_t & count ,
338 const index_t & total ,
342 index_t high_index = source.fetch_sub ( count ) ;
343 index_t low_index = high_index - count ;
345 if ( high_index <= 0 )
351 high = total - low_index ;
352 low = total - high_index ;
357#ifndef VSPLINE_SINGLETHREAD
411template <
bool dummy = true >
417 if ( nr_workers <= 0 )
422 if ( nr_workers == 1 )
435 int count = nr_workers ;
436 std::mutex pool_mutex ;
437 std::condition_variable pool_cv ;
458 std::lock_guard<std::mutex> lk ( pool_mutex ) ;
460 if ( ( -- count ) == 0 )
462 pool_cv.notify_one() ;
472 std::unique_lock<std::mutex> lk_pool ( pool_mutex ) ;
474 vspline_threadpool::common_thread_pool.
launch ( action , nr_workers ) ;
480 pool_cv.wait ( lk_pool , [&] {
return count == 0 ; } ) ;
void launch(std::function< void() > job)
launch simply enqueues a job and calls notify_one. Such a job will run to it's completion and end sil...
definitions common to all files in this project, utility code
const int ncores
number of CPU cores in the system
const int default_njobs
when multithreading, use this number of jobs per default. This looks like overkill and unnecessary si...
bool fetch_ascending(vspline::atomic< index_t > &source, const index_t &total, index_t &index)
fetch_ascending counts up from zero to total-1, which is more efficient if the indexes are used to ad...
int multithread(std::function< void() > payload, std::size_t nr_workers=default_njobs)
multithread uses a thread pool of worker threads to perform a multithreaded operation....
bool fetch_range_ascending(vspline::atomic< index_t > &source, const index_t &count, const index_t &total, index_t &low, index_t &high)
fetch_range_ascending also uses an atomic initialized to the total number of indexes to be distribute...
bool fetch_descending(vspline::atomic< index_t > &source, index_t &index)
fetch_descending fetches the next index from an atomic, counting down. Indexes will range from one le...
bool fetch_range_descending(vspline::atomic< index_t > &source, const index_t &count, index_t &low, index_t &high)
fetch_range_ascending fetches the beginning and end of a range of indexes (in iterator semantic,...
provides a thread pool for vspline's multithread() routine