Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a detached thread and a daemon thread?

I understand that all daemon threads are detached threads, but why are all detached threads not daemon?

Say thread "main" creates thread "A"(non-detached) and thread "A" creates thread "B"(detached). Can thread "A" exit while "B" continues running?

PS: I am asking with reference to pthreads, but please do answer regardless.

like image 938
sgowd Avatar asked Aug 10 '12 15:08

sgowd


People also ask

What is a detached thread?

A Detached thread automatically releases it allocated resources on exit. No other thread needs to join it. But by default all threads are joinable, so to make a thread detached we need to call pthread_detach() with thread id i.e. #include <pthread.h>

What is difference between user thread and daemon thread?

Java offers two types of threads: user threads and daemon threads. User threads are high-priority threads. The JVM will wait for any user thread to complete its task before terminating it. On the other hand, daemon threads are low-priority threads whose only role is to provide services to user threads.

What is difference between daemon and non-daemon thread?

Daemon threads are low priority threads which always run in background and user threads are high priority threads which always run in foreground. User Thread or Non-Daemon are designed to do specific or complex task where as daemon threads are used to perform supporting tasks.

What is daemon thread in Linux?

A “daemon” thread is one that is supposed to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated.


1 Answers

Maybe you should first read What is the difference between fork and thread?

To elaborate more

Daemon Thread

Typically in C/C++ (Linux Environment) one would create a daemon using fork(). fork() creates a new process by duplicating the calling process. Here the parent process would exit leaving the the child process behind. This child process detaches from the controlling terminal, reopens all of {stdin, stdout, stderr} to /dev/null, and changes the working directory to the root directory. (based on flags, of course). Under Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child.

Detached Thread

While Pthread detached behavior is different (1) The detached thread cannot be joined back once detached (2) The detached attribute merely determines the behavior of the system when the thread terminates; it does not prevent the thread from being terminated if the process terminates using exit (or equivalently, if the main thread returns). The pthread_detach() function marks the thread identified by thread as detached. When a detached thread terminates, its resources are automatically released back to the system without the need for another thread to join with the terminated thread.

like image 148
enthusiasticgeek Avatar answered Sep 20 '22 08:09

enthusiasticgeek