在高频率下调用std::async可以吗
Is it OK to call std::async at high frequency?
我写了一个小程序,它使用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();
}
这个循环每秒运行数千次,经过一段随机的时间(5秒-1分钟)后就会崩溃。如果我查看任务管理器,我会发现这个程序的线程数正在快速波动,这让我认为std::async
在每次调用时都会启动新的线程。我本以为它可以用线程池之类的东西。无论如何,这崩溃是因为我做错了什么吗?
使用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 ?? ()
根据要求从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 threwn"; }
}
这是实现中的一个错误。
相关文章:
- 为什么std::async使用同一个线程运行函数
- 为什么可以将左值传递给"std::async",即使它引用了右值
- 使用 std::async 时死锁,将来作为成员
- 为什么我不能将引用作为 std::async 的函数参数传递
- std::async from std::async in windows xp
- std::async 如何工作:为什么它会调用这么多次复制/移动?
- std::async 不会立即调用
- std::async 如果线程是从 DLL 创建的,则会阻止进程退出?
- 使用 std::vector<std::future<int>> 和 std::async 启动几个线程时中止
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 可能的 std::async 实现错误 Windows
- C++从 std::async 函数读取命名空间中的全局变量标志
- std::async 究竟是如何执行的?
- 使用 g++8 和 c++20 的 std::async 编译问题
- 如何有效地使用 std::async 对指针数组执行操作
- 标准C++11是否保证std::async(std::launch::asyncfunc)在单独的线程中启动func
- 何时需要使用std::async(std::launch::asyncfunc())而不是func(()
- std::async([](){ std::cout<< "Hello " ; }) build error
- GCC 的行为与 std::async(std::launch::async) 与 Clang 的行为
- std::async - std::launch::async | std::launch::deferred