c++ - "Bad promise" error after throwing an exception from the thread -
i have function should put in thread may through exception. after throwing exception thread function error.
terminate called after throwing instance of 'std::future_error' what(): broken promise
function worked fine until thought exception. tried not use future , make promise didn't help. here simplified code:
... using namespace std; void myfunc(struct forthread_struct for_thread, promise<const char *> && s_promise); main(){ promise<const char *> v_promise[5]; future<const char *> v_future[5]; thread a_threads[5]; (int_fast8_t k = 0; k < 5; k++ ) { v_future[k] = v_promise[k].get_future(); a_threads[k] = thread(myfunc, for_thread[k], move(v_promise[k])); } (int_fast8_t k = 0; k < 5; ++k) { try { const char * res = v_future[k].get(); } catch(exception & e) { cout<<e.what()<<endl; } } void myfunc(struct forthread_struct for_thread, promise<const char *> && s_promise) { try{ if (for_thread.var == 0){ throw runtime_error("error"); } else{ throw runtime_error("fine"); } } catch(...) { s_promise.set_exception(current_exception()); } }
interesting thing according debugger nothing thrown myfunc
exception thrown main
thread
the promise going out of scope. need take ownership of within thread's function.
this fixes (plus other issues prevented compilation):
#include <future> #include <thread> #include <iostream> using namespace std; struct forthread_struct { int var = 0; }; void myfunc(struct forthread_struct for_thread, promise<const char *> s_promise); int main() { forthread_struct for_thread[5] = {}; promise<const char *> v_promise[5]; future<const char *> v_future[5]; thread a_threads[5]; (int_fast8_t k = 0; k < 5; k++ ) { v_future[k] = v_promise[k].get_future(); a_threads[k] = thread(myfunc, for_thread[k], move(v_promise[k])); } (int_fast8_t k = 0; k < 5; ++k) { try { const char * res = v_future[k].get(); } catch(exception & e) { cout<<e.what()<<endl; } } (auto& t : a_threads) { if (t.joinable()) t.join(); } } void myfunc(struct forthread_struct for_thread, promise<const char *> s_promise) { try{ if (for_thread.var == 0){ throw runtime_error("error"); } else{ throw runtime_error("fine"); } } catch(...) { s_promise.set_exception(current_exception()); } }
expected output:
error error error error error
although version of myfunc might make more sense (and little easier on eyes imho):
void myfunc(struct forthread_struct for_thread, promise<const char *> s_promise) try { if (for_thread.var == 0) { throw runtime_error("error"); } s_promise.set_value("fine"); } catch(...) { s_promise.set_exception(current_exception()); }
update:
fyi, stl library implementors have done hard work packaged_task<>
, std::async<>()
.
your program might arguably more idiomatically implemented thus:
#include <future> #include <thread> #include <iostream> #include <vector> using namespace std; struct forthread_struct { int var = 0; }; const char* myfunc(struct forthread_struct for_thread) { if (for_thread.var == 0) { throw runtime_error("error"); } return "fine"; } int main() { forthread_struct all_structs[] = { { 0 }, { 1 }, { 0 }, { 2 }, { 0 } }; std::vector<std::future<const char *>> futures; (const auto& ts : all_structs) { futures.push_back(std::async(std::launch::async, &myfunc, ts)); } (auto& f : futures) { try { const char * res = f.get(); std::cout << res << std::endl; } catch(exception & e) { cout<<e.what()<<endl; } } }
expected output:
error fine error fine error
Comments
Post a Comment