UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Threads
  • » why new threads exit when main thread exit.

#1 2010-05-22 09:19 PM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: why new threads exit when main thread exit.

In linux, creating thread is same as process (clone()), except the virtual address space gets shared with the parent.

If a running main process(thread) creates new thread, and if main thread exits, why should the new thread too exit? both are different entities,

The same doesn't happen if the child thread exits, the parent thread would be alive.

Could anyone plz clear this doubt..

Offline

#2 2010-05-23 01:40 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: why new threads exit when main thread exit.

There are two system calls to exit in Linux: _exit and exit_group. The first just
exits the process, and in case of a thread only the thread. The second exits all
threads within the thread group. According to POSIX an exit(3) should exit
all the threads, so that's why glibc calls exit_group then. The same is true for
when main returns. If you don't want that you have to call pthread_exit to exit
the main thread (and probably make the other threads detachable).

Offline

  • Index
  • » Threads
  • » why new threads exit when main thread exit.

Board footer

Powered by FluxBB