Thursday, February 2, 2012

P Threads in C

 #include<pthread.h>  
 #include<stdio.h>  
 #include<stdlib.h>  
 #define NUM_THREADS 5  
 int abi = 100;  
 void *PrintHello(void *threadid)  
 {  
  long tid;  
  long tid2;  
  tid = (long)threadid;  
  tid2 = tid;  
  printf("Hello World! its me thread # %ld\n",tid);  
  printf("incrementing tid2 in thread # %ld\n%ld\n",tid,tid2*2);  
  pthread_exit(NULL);  
 }  
 int main(int argc, char *argv[])  
 {  
  pthread_t threads[NUM_THREADS];//array of pthreads  
  int rc;  
  long t;  
  abi++;  
  for(t=0;t<NUM_THREADS;t++)  
   {  
    printf("In main: creating thread %ld\n",t);  
    rc = pthread_create(&threads[t],NULL,PrintHello, (void *)t);  
    if(rc)  
      {  
       printf("Error code returned from code pthread_create() is %d\n",rc);  
       exit(-1);  
      }  
   }  
  pthread_exit(NULL);  
 }  

No comments:

Post a Comment