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

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

1 answer  ·  posted 3y ago by hamsasimon‭  ·  last activity 3y ago by Derek Elkins‭

Question c++ multithreading
#4: Post edited by user avatar hamsasimon‭ · 2020-12-20T18:06:59Z (over 3 years ago)
Clearer wording
  • 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 by user avatar hamsasimon‭ · 2020-12-20T17:59:34Z (over 3 years ago)
Added more code for context.
  • 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 by user avatar hamsasimon‭ · 2020-12-20T17:56:04Z (over 3 years ago)
  • 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 by user avatar hamsasimon‭ · 2020-12-20T17:52:21Z (over 3 years ago)
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()`.