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.

When should I use wait() instead of get() when using C++ threads

+5
−0

I'm trying to understand the purpose of the wait() function in Class future<> for C++. At the moment I don't see any purpose for calling wait() instead of get(). What I have tried in code:

int main()
{
    
    std::future<int>result1(std::async(std::launch::deferred,func1));

    // start synchronously.
    //result1.get();
    result1.wait(); // gives the same output as calling get()

    //print character L
   for (int i = 0; i < 20; ++i)
   {
        std::cout.put('L').flush();
   }

}

here is what's getting called:

int doSomething(char c)
{
  // random-number generator (use c as seed to get different  sequences)
  std::default_random_engine dre(c);
  std::uniform_int_distribution<int> id(10,1000);

  //loop to print character after a random period of time
  for (int i=0; i<10; ++i)
  {
    
    std::cout.put(c).flush();
  }

  return c;
}

int func1 ()
{
  return doSomething('.');
}

But the output I get is the same as I would have had if I called get() instead. That is, func1 gets called prints some characters to the screen and then character is L is printed 20 times.

So, I'm trying to figure out in which situations wait() would be better suited than get().

In the book C++ standard library by Nicolai M. Josuttis, it says that wait() function call allows a background operation to finish without processing its outcome. So, when would we need to do that ?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+5
−0

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »