when to detach or join a boost thread? when to detach or join a boost thread? multithreading multithreading

when to detach or join a boost thread?


It depends what you want to achieve. If you don't care when or if the calls to threadedMethod terminate, or whether or not they throw, then you can just detach the thread as soon as you've created it; each thread will be destroyed when the method completes. And you shouldn't store the thread in a member variable.

If you do care then you need to call join on each thread you create (so once per thread, not once in the destructor). I suspect you don't.

Do you really need to create a new thread for each call? Thread creation can be expensive, so an alternative would be to use a thread pool and submit each threadedMethod invocation to that. The pool could then have the lifetime of the MyClass instance. But perhaps that's overkill for something that is only happening once every 30s.