|
Boost : |
From: Gavin Lambert (boost_at_[hidden])
Date: 2020-03-08 23:54:27
Mere moments ago, quoth I:
> My point about the async calls is that because you're calling async and
> then future.get(), you're blocking the original thread waiting for
> something else, which allows the threadpool work to complete in the
> background, regardless of which threadpool the work was submitted to.
>
> If you completely removed the future.get() calls (even if you still
> called async), then you might end up destroying the threadpool before it
> actually did the work, thus it would appear that submit() did nothing.
> (And the asyncs would also end up doing nothing.)Â But because like I
> said this is up to your OS's thread scheduler, it might sometimes make
> some progress on some of the work before actually being destroyed, which
> can be confusing.
>
> If you have a sleep (or any other work running long enough) in there, so
> that you don't destroy the threadpool too soon, then it will complete in
> the background.
For another example, if you submitted some long-running tasks to ea and
some short-running tasks to ea00, and then only waited on the futures
from ea00, then they'd complete quickly and you'd exit those blocks and
destroy both threadpools -- but ea's work is very likely to be
incomplete or not even started yet.
Conversely, submitting the same tasks to the same pools but instead
waiting on futures from ea instead would increase your chances that both
pools completed their work but it's still not entirely impossible that
only ea finished and ea00 still had some unfinished work. (You'd have
to have quite poor luck with the scheduler though.)
Finally, submitting a task to ea, then async'ing a task also to ea, then
waiting on that second task's future: this would guarantee that both
tasks finished *only if there was exactly one thread in the pool*. If
there's more than one thread then you could still get a situation where
the second task finishes before the first.
If you want to guarantee that everything completes before you stop then
you need to wait for completion of everything in some fashion or another.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk