Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 高频调用std::async可以吗?_C++_Multithreading_C++11_Asynchronous - Fatal编程技术网

C++ 高频调用std::async可以吗?

C++ 高频调用std::async可以吗?,c++,multithreading,c++11,asynchronous,C++,Multithreading,C++11,Asynchronous,我有一个我写的小程序,它使用std::async来实现并行性,它正在崩溃。我很确定有更好的方法可以做到这一点,但现在我只想知道这里发生了什么。我不打算发布确切的代码,因为我不认为这真的有什么区别。它基本上是这样的: while(1) { std::vector<Things> things(256); auto update_the_things = [&](int start, int end) { //some code }; auto han

我有一个我写的小程序,它使用
std::async
来实现并行性,它正在崩溃。我很确定有更好的方法可以做到这一点,但现在我只想知道这里发生了什么。我不打算发布确切的代码,因为我不认为这真的有什么区别。它基本上是这样的:

while(1)
{
    std::vector<Things> things(256);

    auto update_the_things = [&](int start, int end) { //some code };

    auto handle1 = std::async(std::launch::async, update_the_things, 0, things.size() / 4);
    auto handle2 = std::async(std::launch::async, update_the_things, things.size() / 4, things.size() / 4 * 2);
    auto handle3 = std::async(std::launch::async, update_the_things, things.size() / 4 * 2, things.size() / 4 * 3);
    update_the_things(things.size() / 4 * 3, things.size());

    handle1.get();
    handle2.get();
    handle3.get();
}
根据要求,gcc-v的输出:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/tdm-gcc-64/bin/../libexec/gcc/x86_64-w64-mingw32/4.8.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-4.8.1/configure --build=x86_64-w64-mingw32 --enable-targets=all --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-libgomp --enable-lto --enable-graphite --enable-cxx-flags=-DWINPTHREAD_STATIC --enable-libstdcxx-debug --enable-threads=posix --enable-version-specific-runtime-libs --enable-fully-dynamic-string --enable-libstdcxx-threads --enable-libstdcxx-time --with-gnu-ld --disable-werror --disable-nls --disable-win32-registry --prefix=/mingw64tdm --with-local-prefix=/mingw64tdm --with-pkgversion=tdm64-2 --with-bugurl=http://tdm-gcc.tdragon.net/bugs
Thread model: posix
gcc version 4.8.1 (tdm64-2) 

这个符合标准的程序也会崩溃,通常会更快:

#include <iostream>
#include <future>

int main() {
  try {
    for (;;) {
      std::async(std::launch::async, []{}).get();
    }
  } catch(...) { std::cout << "Something threw\n"; }
}
#包括
#包括
int main(){
试一试{
对于(;;){
std::async(std::launch::async,[]{}).get();
}

}catch(…){std::cout“我本以为它可以与线程池或其他东西一起工作”这是完全由实现定义的,您可能应该看到它是如何崩溃的以及在哪里崩溃的,以便能够回答您的问题。您应该特别检查是否是这种情况:
抛出std::system\u错误,错误条件为std::errc::resource\u unavailable\u如果启动策略等于std::launch::async且实现无法执行,请重试要启动一个新线程
@PeterT,我在
std::async
部分放了一个try/catch,但它什么也没有捕获。我只得到一个“此程序已停止工作”消息。我尝试使用gdb,但这会将程序的运行速度降低到不会触发崩溃的程度。这似乎只在循环运行速度特别快时才会发生。gdb确实确认对async的每次调用都会启动一个新线程。哦,你在Windows上使用gcc?是的,我认为gccs的实现目前考虑得不太周到(msvc可能在Win8和更高版本中使用本机线程池机制)。程序崩溃是否没有在事件日志或其他地方显示原因?您是否可以在崩溃时显示
update\u things
的代码?当您执行
catch(std::exception&ex)时,它是否显示了一些内容{std::cout没有解决方案吗?顺便说一句。在Ubuntu Linux上的gcc 4.6.3和4.8.2上没有任何问题。gcc的launch::async实现确实是原始的,只是实例化了一个std::thread,所以这应该可以通过
std::thread t([](){});t.join()复制;
而不是异步调用为什么会因为资源不足而崩溃?这怎么会是一个bug?
#include <iostream>
#include <future>

int main() {
  try {
    for (;;) {
      std::async(std::launch::async, []{}).get();
    }
  } catch(...) { std::cout << "Something threw\n"; }
}