This is a minimal example based on a code that was handed to me:
// Example program
#include <iostream>
#include <string>
#include <thread>
int main()
{
std::cout << "Start main\n";
int i = 0;
auto some_thread = std::thread([&]() {
std::cout << "Start some thread\n";
while(i++ < 100) {
std::cout << i << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
});
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
auto some_thread2 = std::thread([&]() {
std::cout << "Start thread 2\n";
if (some_thread.joinable()) {
std::cout << "Thread 2 will join\n";
some_thread.join();
std::cout << "Thread 2 ended\n";
} else {
std::cout << "Thread 2 ended but didn't join\n";
}
});
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (some_thread.joinable()) {
std::cout << "Main will join\n";
some_thread.join();
std::cout << "Main ended\n";
return 0;
} else {
std::cout << "Main ended but didn't join\n";
return 1;
}
}
In short, the full program (as in this example) access to some std::thread object from different threads and tries to join and detach to it.
I know it's a poor design, and that if you try to join a thread twice it will crash because after the first one finishes, then there's no thread associated with it. But this is not the case.
In this example it's crashing before the thread ends and with an Abort trap:6. It just prints a couple of numbers and then crashes (the thread doesn't end its execution).
The code is not mine and I don't want a redesign. I just want to understand why it's crashing.
A final note, the original code was working somehow using semaphores and such (with some random crashes, though).