Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

77%
+5 −0
Q&A When should I use wait() instead of get() when using C++ threads

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...

posted 3y ago by Derek Elkins‭

Answer
#1: Initial revision by user avatar Derek Elkins‭ · 2020-12-21T02:02:40Z (over 3 years ago)
**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).