简体   繁体   English

可以高频率调用std :: async吗?

[英]Is it OK to call std::async at high frequency?

I have a little program I wrote that uses std::async for parallelism, and it is crashing on me. 我有一个小程序,我写了使用std::async进行并行化,它正在崩溃我。 I'm pretty sure that there are much better ways to do this, but for now I just want to know what is happening here. 我很确定有更好的方法可以做到这一点,但是现在我只想知道这里发生了什么。 I'm not going to post the exact code since I do not think it really makes a difference. 我不会发布确切的代码,因为我不认为它确实有所作为。 It basically looks something like this: 它基本上看起来像这样:

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();
}

This loop runs several thousand times per second and after a random amount of time (5 seconds - 1 minute) it crashes. 这个循环每秒运行几千次,经过一段随机的时间(5秒-1分钟)后,它会崩溃。 If I look in task manager I see that the thread count for this program is rapidly fluctuating, which makes me think that std::async is launching new threads with each call. 如果我查看任务管理器,我会发现该程序的线程数正在迅速波动,这让我觉得std::async正在为每次调用启动新线程。 I would have thought it would work with a thread pool or something. 我本以为它适用于线程池或其他东西。 In any case, is this crashing because I am doing something wrong? 无论如何,这是因为我做错了吗?

Using GDB I get the following: 使用GDB我得到以下内容:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 3560.0x107c]
0x0000000000000000 in ?? ()

#0 0x0000000000000000 in ?? ()
#1 0x000000000041d18c in pthread_create_wrapper ()
#2 0x0000000000000000 in ?? ()

Output from gcc -v as requested: 根据要求从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) 

This standard-conforming program also crashes, and usually much faster: 这个符合标准的程序也会崩溃,通常会更快:

#include <iostream>
#include <future>

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

It's a bug in the implementation. 这是实施中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM