On Nov 1, 2026, at 02:23, Andrzej Krzemienski <akrzemi1@gmail.com> wrote:
niedz., 1 lis 2026 o 05:07 Brook Milligan via Boost <boost@lists.boost.org> napisał(a): I am considering a use of capy that looks like the following:
for (auto i = 0; i < m; ++i) { capy::thread_pool pool(0); for (auto j = 0; j < n; ++j) { capy::run_async(pool.get_executor())(task[j]()); } pool.join(); }
I am wondering about the overhead of constructing/deconstructing a heap of thread pools in the outer loop. Does it make sense to pre-construct m of them prior to the loop and then use the ith one within the loop? Is there a better pattern to use for this?
Is this the most efficient way to execute a bunch of independent tasks in parallel?
This is the first time I am working through capy, so all suggestions are welcome.
I must say, it is very unusual (based on my experience) to want to launch a separate thread pool for each task. Usually, you would have one thread pool per program, or two pools, if you want one group of tasks not to starve the other group. This is also what thread pools are optimised for: perform expensive initialization before the main server loop, upon startup, and then just reuse the threads for different tasks chaply.
Are you sure you need a thread pool per task?
Perhaps you misread the code. There is one pool per group of tasks, with each task in a group posted in the inner loop (perhaps tasks should have been indexed with [i,j]). The idea is that the total number of tasks is partitioned into “m” groups of “n” independent tasks, all “n” of which must complete before the next group starts. My understanding of thread_pool is that joining the threads at the end of each group requires a new thread_pool for the next group, because the first is now unusable. Is that not correct? Instead, is there another way to ensure that one group is complete before the next starts, even with one thread_pool? Thanks. Cheers, Brook