Okay, I encountered this problem before 10 minutes. Below is a code snippet, which SEGFAULTs right at the join()
statement, without any particular reason. As far I'm concerned the code is correct (I had the same problem at a large server application, so I copy-pasted this working snippet from the internet):
#include <iostream>
#include <thread>
int main() {
std::thread t([]() {
std::cout << __LINE__ << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
std::cout << __LINE__ << std::endl;
});
std::cout << __LINE__ << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
t.join();
std::cout << __LINE__ << std::endl;
return 0;
}
What exactly is wrong here? Compiled with...
u@p:~$ g++ --version
g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
...by...
g++ main.cpp -lpthread