我正在尝试一个程序:
#include <iostream>
#include <thread>
#include <future>
int foo() {
return 0;
}
int main(int argc, char* argv[]) {
for (auto i = 0L; i < 10000; ++i) {
auto f = std::async(foo);
f.get();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器VS11 x64.
建立:
cl /EHsc /Zi async.cpp && async
Run Code Online (Sandbox Code Playgroud)
对我来说,这个程序崩溃了.我怀疑,同时实际运行的期货数量有限制.如果我将迭代次数减少到几个订单,它就可以了.
那么,有两个问题:
在C++ 11中实际运行期货有限制吗?
为什么这段代码会崩溃?如果我在"async()"之后立即明确地执行"get()",它必须在下一次迭代之前完成未来,这意味着一次只能运行一个future.
UPDATE
我已将代码简化为:
#include <future>
int main(int argc, char* argv[]) {
for (auto i = 0L; i < 1000000; ++i) {
auto f = std::async([](){ return 0; });
f.get(); …Run Code Online (Sandbox Code Playgroud) #include <string>
#include <cstdio>
int main() {
std::string s = "12345678";
std::printf("[%s]\n", s);
}
Run Code Online (Sandbox Code Playgroud)
这是一个缺少".c_str()"的明显错字.但VS2011即使使用/ Wall也不会发出任何警告,并且程序正常运行.如果要在gcc中编译此代码,它会显示"警告:无法传递非POD类型的对象'struct std :: string'到'...';调用将在运行时中止"并且程序崩溃时出现"非法指令" .
他们真的在VS STL中实现了一个技巧,让程序有这样的拼写错误只是因为这个拼写错误很常见吗?
更新:问题是为什么它在VS中有效?