Wednesday 29 June 2016

Simple Linux Thread Example | Thread c Example

Simple Linux Thread Example | Thread c Example
#include <pthread.h>
#include<stdio.h>
int sum;

void* runner (void *parameters){
   int i,upper=atoi(parameters);
   sum=0;
  
if(upper>0){
      for(i=1;i<=upper;i++)
          sum=sum+i;
   }
   pthread_exit(0);
}

int main(int argc, char *argv[]){
  
   pthread_t threadID;
   pthread_attr_t attributes;
   pthread_attr_init (&attributes);
   pthread_create(&threadID,&attributes,runner,argv[1]);
   pthread_join(threadID,NULL);
   printf("Sum=%d\n",sum);
   return 0;

}

No comments

Post a Comment

Recent Posts