3

I always saw in the internet the rule:

If you don't detach\join a thread, then abort will be called.

I need a reason for why that abort happens.

I can understand with join — because when not doing join to some thread, the the main can be closed before the thread and it can make problems.

But detach doesn't do anything! It has no purpose (at least from what I've seen when running a thread with or without being detached).

What exactly make the abort to jump, any what exactly is the purpose of detach?

Here is a simple example for what causing "aborting":

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds

void pause_thread(int n) 
{
  std::this_thread::sleep_for (std::chrono::seconds(n));
  std::cout << "pause of " << n << " seconds ended\n";
}

int main() 
{
  std::cout << "Spawning and detaching 3 threads...\n";
  std::thread (pause_thread,1);
  std::cout << "Done spawning threads.\n";
  // give the detached threads time to finish (but not guaranteed!):
  pause_thread(5);
  return 0;
}
fdwfg wdfwdfv
  • 229
  • 1
  • 4
  • 9
  • 1
    How come that `detach` doesn't do anything?! Imagine that when the main thread spawns another thread, the latter becomes sorta its slave, dependent on its parent. When you `detach` the child thread, it breaks free and _lives its own life_. – ForceBru Jan 24 '17 at 13:03
  • @ForceBru How do you see that reflecting within a program? everybody who explain me that, tell me the same thing. that the thread is "getting free!!!" but what does it mean????? – fdwfg wdfwdfv Jan 24 '17 at 13:12
  • it means that this thread is now independent from any other threads: if its parent thread is terminated, it continues running, while the 'slave' thread will have to commit suicide in this case. – ForceBru Jan 24 '17 at 13:15
  • A couple of related questions: http://stackoverflow.com/q/19744250/440558 and http://stackoverflow.com/q/6042970/440558. A quick search should give you many more. – Some programmer dude Jan 24 '17 at 13:18

1 Answers1

0

A thread is a different beast from a process. Threads are not in a parent/child relation at all.

C++11 thread implementation has to be compatible with all major OS threads, so it had to make design decisions, which cannot be understood at first sight.

I'll describe pthread, which is used in the linux world.

When you create a thread, you can specify its detachstate. It can be two values:

  • DETACHED: when the thread exits, its allocated resources automatically released. Thread cannot be joined.
  • JOINABLE: when the thread exits, some of its resources are not automatically freed. For example, its return code (in pthread, there is a return value of a thread). Thread can be joined. Resources will be freed at join().

C++ threads are created as JOINABLE, but you can detach it later.

Now, if you don't detach/join a thread, at ~thread(), what could a thread implementation do? It is an issue, because if it doesn't do anything, then some resource will be silently leaked (as a JOINABLE thread when exits, some of its resources are not automatically freed)

  • call join() automatically: not a good idea, as the program can stall on it (if the thread still runs). It is potentially a programming bug.
  • call detach() automatically: not a good idea either, as it could be a programming bug (thread continue to run, but its thread object is destroyed - a programmer should explicitly call detach() in this case)
  • call abort(): this is the best that an implementation can do, to avoid programming errors

So the designers of std::thread chose to call abort() to avoid programming errors.

(On windows, the thread system is similar. You have to call CloseHandle for a thread so its resources can be released)

geza
  • 28,403
  • 6
  • 61
  • 135