Question: These days I am reading the pdf [Designing MT programs](http://www.justsoftwaresolutions.co.uk/files/designing_mt_programs.pdf) . It explains that the user MUST explicitly call `detach()` on an object of class `std::thread` in C++0x before that object gets out of scope. If you don't call it `std::terminate()` will be called and the application will die.

I usually use `boost::thread` for threading in C++. Correct me if I am wrong but a `boost::thread` object detaches automatically when it get out of scope.

Is seems to me that the boost approach follow a RAII principle and the std doesn't. 

Do you know if there is some particular reason for this?

Answer: 
This is indeed true, and this choice is explained in N3225 on a note regarding `std::thread` destructor :

> 
> **If `joinable()` then `terminate()`**, otherwise no effects. [ *Note:*
>  Either implicitly detaching or joining
>  a `joinable()` thread in its
>  destructor could result in difficult
>  to debug correctness (for detach) or
>  performance (for join) bugs
>  encountered only when an exception is
>  raised. **Thus the programmer must
>  ensure that the destructor is never
>  executed while the thread is still
>  joinable**. *—end note* ]
> 
> 
> 

Apparently the committee went for the lesser of two evils.

---

**EDIT** I just found [this interesting paper](http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2008/n2802.html) which explains why the initial wording :

> 
> **If `joinable()` then `detach()`**, otherwise no effects.
> 
> 
> 

was changed for the previously quoted one.
Here's one way to implement RAII threads.

```
#include <memory>
#include <thread>

void run() { /* thread runs here */ }

struct ThreadGuard
{
    operator()(std::thread* thread) const
    {
        if (thread->joinable())
            thread->join(); // this is safe, but it blocks when scoped_thread goes out of scope
        //thread->detach(); // this is unsafe, check twice you know what you are doing
        delete thread;
    }
}

auto scoped_thread = std::unique_ptr<std::thread, ThreadGuard>(new std::thread(&run), ThreadGuard());

```

If you want to use this to detach a thread, [read this first](http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2008/n2802.html).