skip to Main Content

I’m trying to get my simulation running on our high performance server. It (unfortunately) uses CentOS Linux release 7.7.1908 (Core) instead of Win10, under which I’m developing the program. With this came a large amount of errors, one of them I was not able to fix on my on:

#include <future>
#include <iostream>

int main(int argument_count, char** arguments) {
    int i = 1234;
    std::cout << "Initialized i" << std::endl;

    std::promise<int> promise;
    std::cout << "Constructed promise" << std::endl;

    promise.set_value(std::move(i));
    std::cout << "Set value" << std::endl;

    std::future<int> future = std::move(promise.get_future());
    std::cout << "Retrieved future" << std::endl;

    int j = std::move(future.get());
    std::cout << "Got value: " << j << std::endl;

    return 0;
}

When compiling this under Win10 with "cl test.cpp", the output looks like I’d expect:

Desktop>test.exe

Initialized i

Constructed promise

Set value

Retrieved future

Got value: 1234

On the other hand, when compiling on the server with "g++ -std=c++11 test.cpp", the output is different:

~/test_dir$ ./a.out

Initialized i

Constructed promise

terminate called after throwing an instance of ‘std::system_error’

what(): Unknown error -1

Aborted

I do get the same error when trying this with an Ubuntu 16.04.6 LTS machine. I don’t see why this happens.
Obviously, something is fishy in this line: promise.set_value(std::move(i)) since the output before is printed and the line after that statement is not executed any more. Furthermore, the compiler/ linker does find a one of the two versions "void set_value (const T& val);" or "void set_value (T&& val);" that would be appropriate for the template specification "int", i strongly suspect the later.

But why is the program aborting when setting an integer as the value of a promise? Even when inlining the value and skipping the variable all together does produce the error.

Can someone point me to where the error is?

2

Answers


  1. Try compiling using the pthread flag:

    g++ -std=c++11 test.cpp -pthread

    Login or Signup to reply.
  2. Linking against both pthread and stdc++ may resolve the issue.
    Example adding stdc++ in Cmake:

        target_link_libraries(
            # Your library or binary here
            stdc++
            pthread
        )
    
    

    Tested with the following code:

    #include <iostream>
    #include <future>
    
    int main(int argc, char *argv[]) {
        static_cast<void>(argc);
        static_cast<void>(argv);
    
        std::promise<int> pr;
        auto fut = pr.get_future();
        pr.set_value(10);
        std::cout << fut.get() << std::endl;
    
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search