c++ std::future 没有被调用
c++ std::future is not getting called
我正在使用 C++ 中的 std::async
和 std::future
,但遇到了一些麻烦。
当我 运行 这段代码时,我希望看到(在标准输出中) "hello world"
但是,我什么也没得到:
#include <iostream>
#include <future>
using namespace std;
struct A {
future<string>* test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ &a };
}
int main() {
A a = callA();
cout << a.test->get() << endl;
}
我正在使用指向未来的指针,因为在我的真实程序中,我有另一个结构代替 std::string
:
struct A;
struct B;
typedef struct A {
future<B>* b;
} A;
typedef struct B {
A a;
} B;
即使我不使用指针,它也会给我这个错误:
error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = std::__cxx11::basic_string<char>]'
(对于上述错误,我知道我可以使用 std::move 来修复它 here,但我需要使用指针)
那么我怎样才能真正从这个程序中得到 "hello world"
的输出呢?
您使用了指向临时对象的指针。临时对象在函数退出时被销毁。所以你可以将你未来的对象移动到你的结构中:
#include <iostream>
#include <future>
using namespace std;
struct A {
future<string> test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ std::move(a) };
}
int main() {
A a = callA();
cout << a.test.get() << endl;
}
如果出于某种原因你必须使用指针 - 那么你应该延长你未来的生命周期。 (例如,将未来添加到容器中,然后在使用未来后从中删除)
未来会在 callA()
return 秒后立即销毁,因此您有一个指向不再存在的对象的指针。 a.test->get()
因此是未定义的行为,任何事情都可能发生——包括无限期阻塞、崩溃或实际打印正确的结果。
如果您希望从函数中 return 它作为指针,则使用 std::unique_ptr
:
#include <iostream>
#include <future>
using namespace std;
struct A {
unique_ptr<future<string>> test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ make_unique<future<string>>(std::move(a)) };
}
int main() {
A a = callA();
cout << a.test->get() << endl;
}
(Demo)
旁注:using namespace std;
is a bad practice。别这样。
我正在使用 C++ 中的 std::async
和 std::future
,但遇到了一些麻烦。
当我 运行 这段代码时,我希望看到(在标准输出中) "hello world"
但是,我什么也没得到:
#include <iostream>
#include <future>
using namespace std;
struct A {
future<string>* test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ &a };
}
int main() {
A a = callA();
cout << a.test->get() << endl;
}
我正在使用指向未来的指针,因为在我的真实程序中,我有另一个结构代替 std::string
:
struct A;
struct B;
typedef struct A {
future<B>* b;
} A;
typedef struct B {
A a;
} B;
即使我不使用指针,它也会给我这个错误:
error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = std::__cxx11::basic_string<char>]'
(对于上述错误,我知道我可以使用 std::move 来修复它 here,但我需要使用指针)
那么我怎样才能真正从这个程序中得到 "hello world"
的输出呢?
您使用了指向临时对象的指针。临时对象在函数退出时被销毁。所以你可以将你未来的对象移动到你的结构中:
#include <iostream>
#include <future>
using namespace std;
struct A {
future<string> test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ std::move(a) };
}
int main() {
A a = callA();
cout << a.test.get() << endl;
}
如果出于某种原因你必须使用指针 - 那么你应该延长你未来的生命周期。 (例如,将未来添加到容器中,然后在使用未来后从中删除)
未来会在 callA()
return 秒后立即销毁,因此您有一个指向不再存在的对象的指针。 a.test->get()
因此是未定义的行为,任何事情都可能发生——包括无限期阻塞、崩溃或实际打印正确的结果。
如果您希望从函数中 return 它作为指针,则使用 std::unique_ptr
:
#include <iostream>
#include <future>
using namespace std;
struct A {
unique_ptr<future<string>> test;
};
string getStr() {
return "hello world";
}
A callA() {
future<string> a = async(&getStr);
return A{ make_unique<future<string>>(std::move(a)) };
}
int main() {
A a = callA();
cout << a.test->get() << endl;
}
(Demo)
旁注:using namespace std;
is a bad practice。别这样。