Here is my code:
#include <thread>
#include <chrono>
using namespace std::literals::chrono_literals;
#include <iostream>
void f(int n)
{
for (int cnt = 0; cnt < n; ++cnt) {
std::this_thread::sleep_for(1s);
std::cout << "." << std::flush;
}
std::cout << std::endl;
}
int main()
{
std::thread t1 = std::thread(f, 5);
//t1.join();
t1 = std::thread(f, 5); // <- should abort if t1.join() is not done
t1.join();
}
For the website, I am using a gcc9.2 executor to see what happens when an un-joined thread is destructed, but this is content of the compiler output tab:
Could not execute the program
Compiler returned: 1
Compiler stderr
/tmp/ccjlEO57.o: In function `std::thread::thread<void (&)(int), int&, void>(void (&)(int), int&)':
/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/thread:126: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
Also - when I add "-lpthread" to the Compiler options... edit box, I get a different error:
Program returned: 255
Program stderr
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable
Please note that for this 2nd run, the first t1.join()
was not commented out (so it should run fine).
Does this mean you can't test anything std::thread
related on the otherwise incredibly awesome godbolt.org website?