0

My attempt to write a thread pool implementation has stalled out because of issues with the function template responsible for submitting tasks to the pool itself.

I've reduced the code down to its most basic components: creating a std::promise object, assigning to it, and returning its std::future object, and I'm still getting the error.

#include<future>
#include<iostream>

template<typename Func, typename Ret>
void set(std::promise<Ret> & promise, Func && func) {
    promise.set_value(func());
}

template<typename Func>
void set(std::promise<void> & promise, Func && func) {
    func();
    promise.set_value();
}

template<typename Func, typename ... Args>
auto execute(Func && func, Args && ... args) ->
std::future<decltype(func(args...))> {
    using Ret = decltype(func(args...));
    std::promise<Ret> promise;
    set(promise, [&]{return func(std::forward<Args>(args)...);});
    return promise.get_future();
}

void reverse(std::string & string) {
    for(size_t i = 0; i < string.size() / 2; i++)
        std::swap(string[i], string[string.size() - 1 - i]);
}

int main() {
    try{
        std::string string = "Hello World!";
        auto future2 = execute(reverse, string);
        future2.get();
        std::cout << string << std::endl;
    } catch(std::system_error const& e) {
         std::cerr << e.what() << std::endl;
         std::cerr << e.code() << std::endl;
    }
}

Console Output:

Unknown Error: -1
generic: -1

From debugging, the error appears to occur exactly at the moment I call set_value on the promise object; what am I doing wrong?

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • [`std::system_error::code()`](https://en.cppreference.com/w/cpp/error/system_error/code) might be helpful to check what exactly had gone wrong. – πάντα ῥεῖ Feb 14 '19 at 18:10
  • @πάνταῥεῖ If I do `std::cerr << e.code() << std::endl;` I get `generic: -1`. – Xirema Feb 14 '19 at 18:12
  • Did yoiu check the call stack information? – πάντα ῥεῖ Feb 14 '19 at 18:13
  • @πάνταῥεῖ As i said in the post, the error occurs at the moment I call `set_value()` on the `promise` object. – Xirema Feb 14 '19 at 18:14
  • I got `!dlroW olleH` with `g++ 8.2.1`... – Ted Lyngmo Feb 14 '19 at 18:29
  • @TedLyngmo I'm getting the error with the compiler found here: https://www.onlinegdb.com/online_c++_compiler I don't know what compiler they're using. – Xirema Feb 14 '19 at 18:41
  • @Xirema It is G++ 5.4.1, info from FAQ link. Maybe related https://stackoverflow.com/questions/43928715/unknown-exception-from-stdpromise (the same `unknown error -1` on 5.4 version). – rafix07 Feb 14 '19 at 19:56
  • https://ideone.com/myWO6K seems to work. On https://rextester.com/XOC22143 it fails with g++ 5.4, but works with clang. – Ted Lyngmo Feb 14 '19 at 22:03

1 Answers1

0

For unknown reasons, this error appears to be caused by failing to include GCC's thread library, pthread. Compiling the program by adding the -pthread flag causes the error to go away.

Xirema
  • 19,889
  • 4
  • 32
  • 68