Question: Sometime I have to use `std::thread` to speed up my application. I also know `join()` waits until a thread completes. This is easy to understand, but what's the difference between calling `detach()` and not calling it?

I thought that without `detach()`, the thread's method will work using a thread independently.

Not detaching:

```
void Someclass::Somefunction() {
    //...

    std::thread t([ ] {
        printf("thread called without detach");
    });

    //some code here
}

```

Calling with detaching:

```
void Someclass::Somefunction() {
    //...

    std::thread t([ ] {
        printf("thread called with detach");
    });

    t.detach();

    //some code here
}

```

Answer: 
You should call `detach` if you're not going to wait for the thread to complete with `join` but the thread instead will just keep running until it's done and then terminate without having the spawner thread waiting for it specifically; e.g.

```
std::thread(func).detach(); // It's done when it's done

```

`detach` basically will release the resources needed to be able to implement `join`.

It is a fatal error if a thread object ends its life and neither `join` nor `detach` has been called; in this case `terminate` is invoked.
When you detach thread it means that you don't have to `join()` it before exiting `main()`.

*Thread library will actually wait for each such thread **below-main**, but you should not care about it.*

`detach()` is mainly useful when you have a task that has to be done in background, but you don't care about its execution. This is usually a case for some libraries. They may silently create a background worker thread and detach it so you won't even notice it.