如何,在C ++ 11,使用std :: async成员函数? [英] How to, in C++11, use std::async on a member function?
本文介绍了如何,在C ++ 11,使用std :: async成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何对成员函数执行std :: async调用?
How can I operate std::async call on a member function?
示例:
class Person{
public:
void sum(int i){
cout << i << endl;
}
};
int main(int argc, char **argv) {
Person person;
async(&Person::sum,&person,4);
}
我想调用sum async。
I want to call to sum async.
Person p;
call async to p.sum(xxx)
它与std :: async。
不要使用boost。
寻找一行异步调用方式。
I didnt figure out if i can do it with std::async. Dont want to use boost. Looking for a one line async call way.
推荐答案
类似这样:
auto f = std::async(&Person::sum, &p, xxx);
或
auto f = std::async(std::launch::async, &Person::sum, &p, xxx);
其中 p
是 Person
实例和 xxx
是 int
。
这个简单的演示程序使用GCC 4.6.3:
This simple demo works with GCC 4.6.3:
#include <future>
#include <iostream>
struct Foo
{
Foo() : data(0) {}
void sum(int i) { data +=i;}
int data;
};
int main()
{
Foo foo;
auto f = std::async(&Foo::sum, &foo, 42);
f.get();
std::cout << foo.data << "\n";
}
这篇关于如何,在C ++ 11,使用std :: async成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文