Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
tl;dr If you don't have a reason otherwise, you should use get. More precisely, if you will be done with the future, you should use get. You should use wait only if there is some separation between...
Answer
#1: Initial revision
**tl;dr** If you don't have a reason otherwise, you should use `get`. More precisely, if you will be done with the future, you should use `get`. You should use `wait` only if there is some separation between when you want to ensure the visible side-effects of the future have completed and when you will be done with the future. The purpose of `wait` is to cause the thread executing it to block until all visible side-effects of the corresponding future are completed. The purpose of `get` is to get the result (if any) ***or*** throw any exceptions for a failed future (which requires waiting first). After `get` completes, the future is no longer valid, i.e. `get` can be called at most once. This also allows the future to discard some internal state. You should use `wait` if you want to ensure any potential (visible) side-effects of a future are completed before some point in time, but you intend to consume the result later (including throwing any waiting exceptions). You should use `get` when you want to consume the result of the future including throwing any exceptions. Generally, you should use `get` even for `future<void>` as opposed to `wait`. `wait` is to ensure that visible side-effects happen before some other code. In most cases, you might as well get the result at this point, but, particularly for utility code, you may not want to actually consume the results. For example, I may make a utility function to wait on a vector of futures, but I don't actually want to consume the results or cause the exceptions to be thrown. The type of the utility may be: template <typename T> vector< future< T > > &wait_all(vector< future< T > > &futures); Importantly, `wait` can be called as often as one wants as long as the future is valid. This means the above code does not have to worry that some of the futures have already had `wait` called. Focusing on `future<void>`, the differences between `get` and `wait` (assuming a valid future) is that 1) `get` will potentially throw exceptions, and 2) the future is no longer valid after `get` is called (allowing resources to be released).