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

66%
+2 −0
Q&A Confusion about using std::async with class member function

So,I'm having a doubt when I call std:async() on a member function of a class. The book I'm reading says (emphasis mine) You can also pass a pointer to a member function to async(). In that case...

1 answer  ·  posted 4y ago by hamsasimon‭  ·  last activity 4y ago by C. K. Young‭

Question c++ multithreading
#2: Post edited by user avatar hamsasimon‭ · 2020-12-26T03:07:55Z (almost 4 years ago)
Typo in title.
  • Confusion about ssing std::async with class member function
  • Confusion about using std::async with class member function
  • So,I'm having a doubt when I call `std:async()` on a member function of a class. The book I'm reading says (emphasis mine)
  • > You can also pass a pointer to a member function to async(). In that case, the first argument after the member function has to be a **reference** or a **pointer** to the object for which the member function gets called:
  • However, in the example given the object of the class is passed by value, so I thought that perhaps it was a small typo and I decided to tryout myself:
  • #include <future>
  • #include <thread>
  • #include <iostream>
  • // Example of calling a member functions with std::async()
  • class X
  • {
  • public:
  • void add (int num1, int num2)
  • {
  • int result = num1 + num2;
  • std::cout << "The result is " << result << "\n";
  • }
  • };
  • int main()
  • {
  • X x;
  • auto a = std::async(&X::add, x, 42,5); // try call x.add(42,5) asynchronously
  • //auto a = std::async(&X::add, &x, 42,5); // this works as well
  • }
  • It turns out at least on my environment it works if the object is passed by reference and by value. I'm compiling in windows with `g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0`
  • Also, this answer on stack overflow seems to indicate that the object should only be passed by reference: [https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function](https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function)
  • So,I'm having a doubt when I call `std:async()` on a member function of a class. The book I'm reading says (emphasis mine)
  • > You can also pass a pointer to a member function to async(). In that case, the first argument after the member function has to be a **reference** or a **pointer** to the object for which the member function gets called:
  • However, in the example given the object of the class is passed by value, so I thought that perhaps it was a small typo and I decided to tryout myself:
  • #include <future>
  • #include <thread>
  • #include <iostream>
  • // Example of calling a member functions with std::async()
  • class X
  • {
  • public:
  • void add (int num1, int num2)
  • {
  • int result = num1 + num2;
  • std::cout << "The result is " << result << "\n";
  • }
  • };
  • int main()
  • {
  • X x;
  • auto a = std::async(&X::add, x, 42,5); // try call x.add(42,5) asynchronously
  • //auto a = std::async(&X::add, &x, 42,5); // this works as well
  • }
  • It turns out at least on my environment it works if the object is passed by reference and by value. I'm compiling in windows with `g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0`
  • Also, this answer on stack overflow seems to indicate that the object should only be passed by reference: [https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function](https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function)
#1: Initial revision by user avatar hamsasimon‭ · 2020-12-26T03:06:46Z (almost 4 years ago)
Confusion about ssing std::async with class member function
So,I'm having a doubt when I call `std:async()` on a member function of a class. The book I'm reading says (emphasis mine)
 > You can also pass a pointer to a member function to async(). In that case, the first argument after the member function has to be a **reference** or a **pointer** to the object for which the member function gets called:

However, in the example given the object of the class is passed by value, so I thought that perhaps it was a small typo and I decided to tryout myself:

    #include <future>
    #include <thread>
    #include <iostream>


    // Example of calling a member functions with std::async()
    class X
    {
    public:
      void add (int num1, int num2)
      {
          int result = num1 + num2;

          std::cout << "The result is " << result << "\n";
      }

    };

    int main()
    {
      X x;
      auto a = std::async(&X::add, x, 42,5); // try call x.add(42,5) asynchronously
    //auto a = std::async(&X::add, &x, 42,5); // this works as well
    }

It turns out at least on my environment it works if the object is passed by reference and by value. I'm compiling in windows with `g++.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0`

Also, this answer on stack overflow seems to indicate that the object should only be passed by reference: [https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function](https://stackoverflow.com/questions/13669094/how-to-use-stdasync-on-a-member-function)