3

I have the following simple function, just for demonstration:

void thread_func(int i) {
    return i;
}

And I have another function that calls it in a new thread and does not wait for it to return:

void my_nice_function() {
    std::thread t(thread_func, 15);
}

Since t is a local variable in my_nice_function(), I am concerned that when my_nice_function() exits, t will be destroyed while running.

Is there a reason to be concerned? How can I let the thread t run in background even after returning from my_nice_function()?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 2
    You must call `join()` or `detach()` on `t` before it's destroyed, otherwise `~thread` destructor calls `terminate()` and therefore aborts the program. – Igor Tandetnik Oct 30 '17 at 12:58
  • 2
    Possible duplicate of [When should I use std::thread::detach?](https://stackoverflow.com/questions/22803600/when-should-i-use-stdthreaddetach) – nvoigt Oct 30 '17 at 12:59
  • I don't think it is a duplicate since not everybody know what `std::detach` is. Using `std::detach` seems to be the answer here – SomethingSomething Oct 30 '17 at 13:00
  • @SomethingSomething, The link explains what `detach` does. People with this problem have their question answered by the link. – chris Oct 30 '17 at 13:04
  • The answer for my question is not the answer for what `detach()` does, but is that I should use `detach()` - a function that I haven't known before and possibly also other developers don't. I agree that pointing to there as an answer and telling "use detach() and read that post" is fine, but it is not a duplicate question – SomethingSomething Oct 30 '17 at 14:43

2 Answers2

4

Your concern is valid. Reading suitable reference documentation for std::thread tells us that on destruction:

If *this has an associated thread (joinable() == true), std::terminate() is called.

So destroying t without either detaching or joining it will end your program.

You therefore have two options, depending on what you want to happen. If you want the function to wait for the thread to finish, call t.join() before returning. If you instead want the thread to keep running after the function returns, call t.detach().

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    So the solution for my case is to add `t.detach()` after creating `t`, right? (Given that I don't want to wait for the thread to finish, but I do want it to run in the background) – SomethingSomething Oct 30 '17 at 13:02
2

Yes it is a reason of concern. The code will not work as intended. You can either do

t.join ()

This should be written if you want your main function to wait till execution of the threaded func. is completed. Or

t.detach ()

This should be called if you want the threaded func. to run independently, i mean main func. should not wait for it complete.

You can also use Sleep (x). If you want the function to run for x seconds only. Calling terminate() is generally not recommended as it kills all threads including main () thread. There is no need to kill a thread. When the finction running in a thread completes the threads automatically cleans itself up.

Refer to this: How to Destroy a thread object

Vedant
  • 145
  • 1
  • 13