mercredi 29 janvier 2020

MWE of std::thread::join on cppreference.com and cplusplus.com produce segmentation faults

On my computer, the examples given at: https://en.cppreference.com/w/cpp/thread/thread/join and http://www.cplusplus.com/reference/thread/thread/join/ produce a segmentation fault.

The code below print "foo", then gdb report a segmentation fault at t.join(). If I replace join with detach, I get a segmentation fault at detach.

#include <thread>
void f() {std::cout << "foo" << std::endl;}

int main(int argc, char* argv[]) {
    std::thread t(f);
    t.join();
}

Similarly, in the code below, gdb report that s.get() calls std::thread::join(), which in turn produces a segmentation fault. However, replacing std::launch::async by std::launch::deferred fix it (no thread is created).

#include <future>
std::string f() {return "foo";}

int main(int argc, char* argv[]) {
    std::future<std::string> s = std::async(std::launch::async, f);
    std::cout << s.get() << std::endl;
}

The culprit might be my computer, however, it is very plain : Ubuntu 18.04, gcc 7.4. The code itself is compiled with -pthread and std=c++1y. The linker has the -lpthread flag. With such a normal configuration, if I get this problem, then most people on SO should have the same problem, but it does not seem to be the case.

What did I miss ?

Aucun commentaire:

Enregistrer un commentaire