Switch to full style
For C/C++ coders discussions and solutions
Post a reply

daemon thread - What is the use of deamon thread?

Fri Oct 17, 2008 1:24 pm

Daemon term is mainly used in UNIX. In Java, this is used to indicate a special type of thread. Normally when a thread is created in Java, by default it is a non-daemon thread. Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.
A daemon thread should be used for some background task that might provide a service to the applications. e.g. a Server thread listening on a port for the clients` requests. A thread for which an exit method is not provided or which does not have an exit mechanism can be marked as a daemon thread.

Note that the normal C++ code is like this :
Code:
#include <iostream>
#include <pthread.h>

using namespace std;

#define threadCount     6

void *myThreadFunc(void *threadArg)
{
   long numberOfThread;
   numberOfThread = (long)threadArg;
   cout << "  Thread number now is:" << numberOfThread << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threadArray[threadCount];
   int status;
   int i;
   for( i=0; i < threadCount; i++ ){
      cout << "main()  - starting thread: #" << i << endl;
      status = pthread_create(&threadArray[i], NULL, 
                          myThreadFunc
, (void *)i);
      if (status){
         cout << "error in creating a thread ," << status << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}
 


with output:
Code:
main()  - starting thread: # 0
main()  - starting thread: # 1
main()  - starting thread: # 2
main()  - starting thread: # 3
main()  - starting thread: # 4
main()  - starting thread: # 5
Thread number now is: 0
Thread number now is: 1
Thread number now is: 2
Thread number now is: 3
Thread number now is: 4
Thread number now is: 5




Re: daemon thread - What is the use of deamon thread?

Tue Aug 11, 2009 12:01 am

thank you for this information . :)

Re: daemon thread - What is the use of deamon thread?

Tue Aug 11, 2009 5:17 am

Daemon term is mainly used in UNIX. In Java, this is used to indicate a special type of thread. Normally when a thread is created in Java, by default it is a non-daemon thread. Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.

Re: daemon thread - What is the use of deamon thread?

Tue Aug 11, 2009 8:32 am

thank you this is a good addaition .

Post a reply
  Related Posts  to : daemon thread - What is the use of deamon thread?
 Thread Safe     -  
 Thread of Event Dispatcher     -  
 Multi Thread Program     -  
 initial state of thread     -  
 invokes a thread's run() method     -  
 thread cannot acquire a lock on an object     -  
 thread state when it terminates its processing     -  
 Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW     -  
 high-level thread states     -  

Topic Tags

C++ Threads