This is a continuation to my question: Thread creation inside constructor
Now that I have successfully created a thread in a constructor, I know that it must be joined. As I am developing an API, I have no control over the main function so I can't join it there.
Will it be right to join the thread in the class destructor, considering that the instanced object of that class would have an application's lifetime?
You could do that. However, are you really interested in the mechanism of launching a new thread, or are you perhaps interested in the effect of performing something asynchronously? If it's the latter, you can move to the higher level
asyncmechanism:You're asking in the constructor to launch
barasynchronously. You don't care about managing the thread yourself - let the library handle that.Place the resulting
futurein a member.In the dtor,
getthe future.