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
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: i...
#4: Post edited
I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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 ! i.e 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 if there is any use of calling `wait()`.- 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 ?
- 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 ?
#3: Post edited
- I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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();
- }
- }
- But the output I get is the same ! i.e 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 if there is any use of calling `wait()`.
- 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 ?
- I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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 ! i.e 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 if there is any use of calling `wait()`.
- 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 ?
#2: Post edited
- I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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();
- }
- }
- But the output I get is the same ! i.e 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 if there is any use of calling `wait()`.
- I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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();
- }
- }
- But the output I get is the same ! i.e 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 if there is any use of calling `wait()`.
- 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 ?
#1: Initial revision
When should I use wait() instead of get() when using C++ threads
I'm trying to understand the purpose of the `wait()` function in Class future<> for C++. I'm trying to know when would I use `wait()` instead of `get()`. What I have tried in my 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(); } } But the output I get is the same ! i.e 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 if there is any use of calling `wait()`.