Question: I'm new to C++ and I'm trying to make the console print "after 5 seconds" after 5000 ms. Then print "insta log" immediately after the new thread's declaration.

But doing so crashes with the following error:

"Debug Error!

[PROGRAM PATH]

abort() has been called

"

This is my code:

```
#include <iostream>
#include <thread>
#include <Windows.h>
#include <ctime>

using namespace std;

void f() {
    Sleep(5000);
    cout << "after 5 seconds" << endl;
}

int main() {
    cout << "starting" << endl;

    // Pass f and its parameters to thread 
    // object constructor as
    thread t(&f);
    cout << "insta log" << endl;

}

```

I'm unsure why this is happening. I've searched around and I found a "fix" but it makes my code not behave as intended.

This is the "fix"

```
#include <iostream>
#include <thread>
#include <Windows.h>
#include <ctime>

using namespace std;

void f() {
    Sleep(5000);
    cout << "after 5 seconds" << endl;
}

int main() {
    cout << "starting" << endl;

    // Pass f and its parameters to thread 
    // object constructor as
    thread t(&f);

    t.join();

    cout << "insta log" << endl;    // doesn't print for 5 seconds

}

```

This removes the error message but yields the main thread for 5 seconds. Which makes my code not work as intended.

Thanks in advance, any help is appreciated!

Answer: 
If you had clicked 'retry' in that dialog you would have seen why your code died. Its right here

```
~thread() noexcept {
    if (joinable()) {
        _STD terminate();
    }
}

```

in the MSVC implementation of std::thread. The code says that destroying a thread thats still joinable (ie is running) is illegal. I dont know if thats c++ defined behavior, a quick search didnt show me. Anyway when you `join` the thread, you will wait till its OK to destroy it
Functions are already passed around as pointers, use `thread t(f)` instead of `thread t(&f)`.

Moreover, since your `main()` neither lasts longer than the thread or calls a `t.join()`, the program will end before the thread finishes it's code, so that might be another reason for a crash. In fact it is probably the reason for the crash.

If you want `"insta log"` to print instantly, then call `t.join()` at the end of `main()`. `t.join()` will wait for the thread `t` to end before continuing.