Can std :: async be called at high frequency?

I have a little program that I wrote that uses std::async

parallelism and it crashes on me. I'm pretty sure there are much better ways to do this, but for now, I just want to know what's going on here. I won't post the exact code as I don't think it really matters. 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 time (5 seconds - 1 minute) it will work. If I look in the task manager, I see that the number of threads for this program fluctuates rapidly, which makes me think that it is std::async

starting new threads on every call. I would have thought this would work with a thread pool or something. Is it a crash anyway because I am doing something wrong?

Using GDB I get the following:

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:

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) 

      

+3


source to share


1 answer


This ss-compatible program also crashes and is usually much faster:

#include <iostream>
#include <future>

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

      



This is an implementation bug.

+4


source







All Articles