它是确定在高频调用的std ::异步? [英] Is it OK to call std::async at high frequency?
问题描述
我有一个小程序,我写了一个使用的std ::异步
并行性,它崩溃在我身上。我是pretty确保有更好的方法可以做到这一点,但现在我只想知道这里发生了什么。我没有要发布的确切code,因为我不认为这真的有差别。基本上,它看起来是这样的:
而(1)
{
的std ::矢量<&东西GT;事(256); 汽车update_the_things = [&放大器;(INT开始,诠释完){//一些code}; 自动=句柄1的std ::异步(的std ::推出::异步,update_the_things,0,things.size()/ 4);
汽车handle2 =的std ::异步(的std ::推出::异步,update_the_things,things.size()/ 4,things.size()/ 4 * 2);
汽车handle3 =的std ::异步(的std ::推出::异步,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 ::异步
将推出每次调用新主题。我本来以为会用一个线程池什么工作。在任何情况下,这种崩溃,因为我做错了什么?
使用GDB我得到以下几点:
计划接收信号SIGSEGV,分割过错。
[切换主题3560.0x107c]
0x0000000000000000在?? ()#0 0x0000000000000000的? ()
#1 0x000000000041d18c在pthread_create_wrapper()
#2 0x0000000000000000的? ()
这是按要求GCC -v输出:
使用内置的规格。
COLLECT_GCC = GCC
COLLECT_LTO_WRAPPER = C:/ TDM-GCC-64 / bin中/../的libexec / GCC / x86_64的-W64-的mingw32 / 4.8.1 / LTO-wrapper.exe
目标:x86_64的-W64-的mingw32
../../../src/gcc-4.8.1/configure --build = x86_64的-W64-的mingw32 --enable-目标=所有--enable-语言= ADA,C,C ++,FORTRAN:与配置,LTO,objc,OBJ-C ++ --enable-libgomp --enable-LTO --enable-石墨--enable-CXX-标志= -DWINPTHREAD_STATIC --enable-libstdcxx调试--enable-线程= POSIX --enable -version-特定运行时库--enable-完全动态的串--enable-libstdcxx线程--enable-libstdcxx时间--with-GNU-LD --disable-werror --disable-NLS - 禁用-Win32的注册表 - preFIX = / mingw64tdm --with-局域preFIX = / mingw64tdm --with-pkgversion = tdm64-2 --with-bugurl = HTTP:// TDM-GCC。 tdragon.net/bugs
线程模型:POSIX
gcc版本4.8.1(tdm64-2)
这符合标准的程序也崩溃了,平时要快得多:
的#include<&iostream的GT;
#包括LT&;&未来GT;诠释主(){
尝试{
为(;;){
。的std ::异步(的std ::推出::异步,[] {})获得();
}
}赶上(...){性病::法院LT&;< 扔东西\\ N的; }
}
这是在执行中的错误。
I have a little program I wrote that uses std::async
for parallelism, and it is crashing on me. 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. 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. 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:
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)
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.
这篇关于它是确定在高频调用的std ::异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!