adsense


Friday, 27 November 2015

C++ LANGUAGE JOINING AND DETACHING THREADS

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE JOINING AND DETACHING THREADS




Joining and Detaching Threads 


There are following two routines which we can use to join or detach threads: 

pthread_join (threadid, status)  

pthread_detach (threadid)  

The pthread_join() subroutine blocks the calling thread until 
the specified ‘threadid’ thread terminates. When a thread is 
created, one of its attributes defines whether it is joinable 
or detached. Only threads that are created as joinable can be 
joined. If a thread is created as detached, it can never be joined.
 
This example demonstrates how to wait for thread completions by using the Pthread join routine.
 
#include <iostream> 
#include <cstdlib> 
#include <pthread.h> 
#include <unistd.h> 
 
using namespace std; 
 
#define NUM_THREADS     5 
 
void *wait(void *t) 
{ 
   int i; 
   long tid; 
 
   tid = (long)t; 
 
   sleep(1); 
   cout << "Sleeping in thread " << endl; 
   cout << "Thread with id : " << tid << "  ...exiting " << endl; 
   pthread_exit(NULL); 
} 
 
int main () 
{ 
   int rc; 
   int i; 
   pthread_t threads[NUM_THREADS]; 
   pthread_attr_t attr; 
   void *status; 
 
   // Initialize and set thread joinable 
   pthread_attr_init(&attr); 
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); 
 
   for( i=0; i < NUM_THREADS; i++ ){ 
      cout << "main() : creating thread, " << i << endl; 
      rc = pthread_create(&threads[i], NULL, wait, (void *)i ); 
      if (rc){ 
         cout << "Error:unable to create thread," << rc << endl; 
         exit(-1); 
      } 
   } 
 
   // free attribute and wait for the other threads 
   pthread_attr_destroy(&attr); 
   for( i=0; i < NUM_THREADS; i++ ){ 
      rc = pthread_join(threads[i], &status); 
      if (rc){ 
         cout << "Error:unable to join," << rc << endl; 
         exit(-1); 
      } 
      cout << "Main: completed thread id :" << i ; 
      cout << "  exiting with status :" << status << endl; 
   } 
 
   cout << "Main: program exiting." << endl; 
   pthread_exit(NULL); 
} 
When the above code is compiled and executed, it produces the following result: 
main() : creating thread, 0 
main() : creating thread, 1 
main() : creating thread, 2 
main() : creating thread, 3 
main() : creating thread, 4 
Sleeping in thread 
Thread with id : 0 .... exiting 
Sleeping in thread 
Thread with id : 1 .... exiting 
Sleeping in thread 
Thread with id : 2 .... exiting 
Sleeping in thread 
Thread with id : 3 .... exiting 
Sleeping in thread 
Thread with id : 4 .... exiting 
Main: completed thread id :0  exiting with status :0 
Main: completed thread id :1  exiting with status :0 
Main: completed thread id :2  exiting with status :0 
Main: completed thread id :3  exiting with status :0 
Main: completed thread id :4  exiting with status :0 
Main: program exiting.


No comments: