capy: thread_pool use
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. Thanks a lot. Cheers, Brook
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.
Hi Brook, 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? Also, the cost of starting a thread pool is not only memory directly, but launching threads (which includes preparing stack memory for them). Regards, &rzej;
Thanks a lot.
Cheers, Brook
_______________________________________________ Boost mailing list -- boost@lists.boost.org To unsubscribe send an email to boost-leave@lists.boost.org https://lists.boost.org/mailman3/lists/boost.lists.boost.org/ Archived at: https://lists.boost.org/archives/list/boost@lists.boost.org/message/YWRNNL36...
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
On Sun, Nov 1, 2026 at 8:49 AM Brook Milligan via Boost < boost@lists.boost.org> wrote:
I am wondering about the overhead of constructing/deconstructing a heap of thread pools in the outer loop.
Seems fine to me.
...is there another way to ensure that one group is complete before the next starts, even with one thread_pool?
You could build a synchronizer which counts down for each task and then unblocks. Your function with the loops would have to be a coroutine. This would let you construct the thread pool once (or even pass it in as a parameter). Thanks
On Nov 1, 2026, at 10:54, Vinnie Falco via Boost <boost@lists.boost.org> wrote:
On Sun, Nov 1, 2026 at 8:49 AM Brook Milligan via Boost < boost@lists.boost.org> wrote:
I am wondering about the overhead of constructing/deconstructing a heap of thread pools in the outer loop.
Seems fine to me.
I interpret this to mean that it is correct code, but (given other comments) perhaps not the most efficient.
...is there another way to ensure that one group is complete before the next starts, even with one thread_pool?
You could build a synchronizer which counts down for each task and then unblocks. Your function with the loops would have to be a coroutine. This would let you construct the thread pool once (or even pass it in as a parameter).
OK, good idea. I’m now beginning to understand when_all(), and see that it can take a vector of tasks. That would seem to handle the inner loop. Does this seem like the right path forward? capy::thread_pool pool(0); capy::run_async(pool.get_executor()) ( [=]() -> capy::task<> { std::vector<capy::task<capy::results<>>> jobs; for (auto i = 0; i < m; ++i) { jobs.reserve(n); for (int j = 0; j < n; ++j) jobs.push_back(make_job(i,j)); auto [ec] = coawait capy::when_all(std::move(jobs)); } }()); pool.join(); Thanks a lot for your help. Both coroutines and capy are new to me, so I appreciate the suggestions. Cheers, Brook
On Sun, Nov 1, 2026 at 11:17 AM Brook Milligan <brook@biology.nmsu.edu> wrote:
I interpret this to mean that it is correct code, but (given other comments) perhaps not the most efficient.
I would avoid getting too worked up over whether it is "proper" or "efficient" until you have a measurement that indicates otherwise. From what it sounds like, you have a pattern that works and does what you need. Use it.
...when_all()... Does this seem like the right path forward?
Yes this does sound right, if it works Thanks
participants (3)
-
Andrzej Krzemienski -
Brook Milligan -
Vinnie Falco