115#ifndef __ADS_ThreadPool
116 #define __ADS_ThreadPool
161 explicit ThreadPool( std::size_t p = 0 , std::int64_t spin = 2000000 ,
162 std::int64_t maxSpin = 200000000 );
173 return( f_slot.size() );
210 void bulk( std::size_t n , F && f );
235 template<
class F ,
class G >
270 void run( std::size_t p , F && f );
277 void ( *
run )( Task * )
noexcept;
281 struct alignas( 128 ) Slot
284 std::uint64_t seed = 0;
304 void loop( std::size_t s );
309 bool lookFor( P & ready , std::chrono::steady_clock::time_point t0 ,
310 std::int64_t budget );
313 Task * find( std::size_t s );
316 bool help( std::size_t s );
322 static constexpr std::int64_t kLongestSpin = std::int64_t( 1 ) << 58;
325 static inline thread_local Self tl_self{ nullptr , 0 };
327 std::vector< std::unique_ptr< Slot > > f_slot;
328 std::vector< std::thread > f_thread;
330 std::atomic< bool > f_stop{
false };
333 std::int64_t f_maxSpin;
335 std::mutex f_runLock;
336 std::atomic< bool > f_runOpen{
false };
337 std::size_t f_runNext = 0;
338 std::size_t f_runN = 0;
339 void ( *f_runCall )(
void * , std::size_t ) =
nullptr;
340 void * f_runCtx =
nullptr;
341 std::atomic< std::size_t > f_runDone{ 0 };
352#include "ADS/parallel/impl/ThreadPool.tpp"
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free struc...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
The deque of work stealing of Chase and Lev, beyond the book: its owner thread pushes and takes at th...
void invoke(F &&f, G &&g)
the calls f() and g() in parallel, returning when both have returned
std::size_t concurrency(void) const
the number of threads, the caller included
Definition ThreadPool.h:172
void run(std::size_t p, F &&f)
the calls of long-lived workers, each begun by a thread of the pool that holds no other one,...
std::mutex lock_type
the lock of the data shared by the workers of run()
Definition ThreadPool.h:239
~ThreadPool()
stops the workers, which first finish their tasks
ThreadPool & operator=(const ThreadPool &)=delete
no copy, since the workers hold the address of the pool
ThreadPool(std::size_t p=0, std::int64_t spin=2000000, std::int64_t maxSpin=200000000)
a pool of threads, the caller included (the number of the hardware threads if ), whose idle workers ...
void bulk(std::size_t n, F &&f)
the calls on the threads of the pool, returning when all have returned
ThreadPool(const ThreadPool &)=delete
no copy, since the workers hold the address of the pool