g++ - C++ std::async segfault only when using std::launch::async policy -


the following code produces segfault on function call (valgrind trace follows).

groovefeat procgroove(double tradius); 

...

std::future<groovefeat> *features = new std::future<groovefeat>[evalcount]; for(unsigned int = 0; < evalcount; i++) {   double effradius = evalmin + evalstep*i;   std::cout << "allocating feature " << << "\n";   features[i] = std::async(std::launch::async, &procgroove, effradius); }  for(unsigned int = 0; < evalcount; i++) {   std::cout << "waiting on evaluation " << << "/" << evalcount << "\n";   groovefeat feat = features[i].get();    ... } delete[] features; 

when code reached, see segfault between allocation of features 1-5 (the exact feature varies execution execution). if evalcount manually set 1, same error persists thread activated.

allocating feature 0 allocating feature 1 allocating feature 2 ==27135== thread 2: ==27135== invalid read of size 8 ==27135==    @ 0x403b39: procgroove(double) (in profilparr) ==27135==    0x40552b: std::_function_handler<std::unique_ptr<std::__future_base::_result_base, std::__future_base::_result_base::_deleter> (), std::__future_base::_task_setter<std::unique_ptr<std::__future_base::_result<groovefeat>, std::__future_base::_result_base::_deleter>, std::_bind_simple<groovefeat (*(double))(double)>, groovefeat> >::_m_invoke(std::_any_data const&) (in /profilparr) ==27135==    0x405608: std::__future_base::_state_basev2::_m_do_set(std::function<std::unique_ptr<std::__future_base::_result_base, std::__future_base::_result_base::_deleter> ()>*, bool*) (in profilparr) ==27135==    0x4e43dce: __pthread_once_slow (in /usr/lib/libpthread-2.21.so) ==27135==    0x405feb: std::thread::_impl<std::_bind_simple<std::__future_base::_async_state_impl<std::_bind_simple<groovefeat (*(double))(double)>, groovefeat>::_async_state_impl(std::_bind_simple<groovefeat (*(double))(double)>&&)::{lambda()#1} ()> >::_m_run() (in profilparr) ==27135==    0x510c34f: execute_native_thread_routine (thread.cc:84) ==27135==    0x4e3d353: start_thread (in /usr/lib/libpthread-2.21.so) ==27135==    0x59d6bfc: clone (in /usr/lib/libc-2.21.so) ==27135==  address 0xffffffffffffffc8 not stack'd, malloc'd or (recently) free'd 

thus, looks segfault caused standard library, since execution ends before part of procgroove executed. compile command is:

g++ -o3 -pthread -std=c++11 main.cpp -o profilparr -lpthread 

on whim, confirmed executable has same errors regardless of optimization level (ie, -o0). idea how go debugging this? appears there bug report on similar problem several years ago (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54297), marked resolved (i'm using v5.1.0).

edit: removing std::launch::async policy specifier allows program execute without error. why??? however, seeing defaults deferred processing, loses semblance executing procgroove() in parallel. ways around this?


Comments