Skip to content

Latest commit

History

32 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

C++ Thread Pool

Example Usage (Multiple ThreadPool Task Dependencies)

threadpool io_pool(3); // IO tasks will be blocking
threadpool cpu_pool(2);
auto read_rv = io_pool.submit<IO_Data>( []() { returnread(); });
auto parse_rv = read_rv.then( cpu_pool, [](IO_Data data) { returnparse(data); });
auto compress_rv = parse_rv.then(cpu_pool, [](IO_Data data) { returncompress(data); });
auto upload_rv = compress_rv.then(cpu_pool, [](IO_Data data) { returnupload(data); });
io_pool.shutdown();
cpu_pool.shutdown();

Example Usage (Dependency Jobs)

threadpool tp(2);
// Simulated ETL pipelineauto api_a = tp.submit<data>([] { returnfetch_api_a(); });
auto api_b = tp.submit<data>([] { returnfetch_api_b(); });
auto clean_a = api_a.then(tp, [](data d) {
returnclean_api_a(d);
});
auto clean_b = api_b.then(tp, [](data d) {
returnclean_api_b(d);
});
auto merge = tp.when_all(clean_a, clean_b)
.then(tp, [](data a, data b) {
returnmerge_data(a, b);
});
auto analysis = merge.then(tp, [](data m) {
returnrun_analysis(m);
});

Example Usage (No Dependency Jobs)

threadpool tp(4);
std::vector<return_value_handle<int>> futures = {
tp.submit<int>( []() -> int { returnrecursive_fibonacci(10);} ),
tp.submit<int>( []() -> int { returnrecursive_fibonacci(20);} ),
tp.submit<int>( []() -> int { returnrecursive_fibonacci(30);} ),
tp.submit<int>( []() -> int { returnrecursive_fibonacci(40);} ),
};
tp.shutdown();
for (int i=0; i<futures.size(); i++) {
constauto& f = futures[i];
if (f.is_valid()) {
std::cout << "Result " << i << "" << f.get() << std::endl;
} else {
std::cout << "Result " << i << " not available" << std::endl;
}
}
/* OUTPUT Result 0 55 Result 1 6765 Result 2 832040 Result 3 102334155*/

About

Custom Dependency-aware Thread Pool

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages