Skip to content

A C++ class that facilitates the parallel coordinated execution of an arbitrary function

License

Notifications You must be signed in to change notification settings

BYUHPC/CoordinatedLoopingThreadpoolCXX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

C++20 Coordinated Looping Threadpool

A CoordinatedLoopingThreadpool constitutes zero or more threads, each of which executes the same function on a unique argument each time the threadpool is triggered.

Member Functions

Function Purpose
CoordinatedLoopingThreadpool(auto F, auto args) create a threadpool that will execute F(arg) for each arg in args each time the threadpool is triggered.
trigger() run F on each of the supplied arguments one time asynchronously
sync() block until all running instances of F complete
trigger_sync() trigger synchronously; equivalent to trigger(); sync();

sync(), trigger(), and trigger_sync() can be safely called willy-nilly--repeated calls to sync() without corresponding calls to trigger() have no ill effect, and trigger() will block if the previous iteration is still underway.

Usage

Given function F, iterable A, and unrelated function do_stuff(), the following code blocks will yield equivalent results:

for (auto a: A) F(a);
do_stuff();
for (auto a: A) F(a);
do_stuff();
CoordinatedLoopingThreadpool tp(F, A);
tp.trigger_sync();
do_stuff();
tp.trigger_sync();
do_stuff();
CoordinatedLoopingThreadpool tp(F, A);
tp.trigger();
do_stuff();
tp.sync();
tp.trigger();
do_stuf();
tp.sync();

The last block will be fastest if F(a) doesn't take significantly longer than do_stuff() does. If there are multiple cores available and F(a) takes long enough to overcome the overhead of threads, the second and third blocks will be faster than the first.

About

A C++ class that facilitates the parallel coordinated execution of an arbitrary function

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages