Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <malloc.h>
- struct range
- {
- int threadnumber;
- unsigned long start;
- unsigned long end ;
- };
- void * function(void *ptr)
- {
- struct range *rptr;
- rptr=(struct range *)ptr;
- printf("\n Thread number: %d,start : %lu,end :%lu",rptr->threadnumber,rptr->start,rptr->end);
- }
- int main()
- {
- pthread_t *threadid;
- int i,j,threadcount;
- unsigned long start[3];
- unsigned long end[3];
- struct range **Rangearr;
- printf("\n Enter the number of threads to run : ");
- scanf("%d",&threadcount);
- Rangearr=(struct range **)malloc(threadcount*sizeof(struct range *));
- threadid=(pthread_t *)malloc(threadcount * sizeof(pthread_t) );
- for(i=0;i<threadcount;i++)
- {
- start[i]=i;
- end[i]=i+3;
- threadid[i]=i;
- }
- for(i=0;i<threadcount;i++)
- {
- Rangearr[i]=(struct range *)malloc(sizeof(struct range));
- Rangearr[i]->start=start[i];
- Rangearr[i]->end=end[i];
- Rangearr[i]->threadnumber=i;
- }
- for(i=0;i<threadcount;i++)
- {
- pthread_create(&threadid[i],NULL,function,(void *) Rangearr[i]);
- }
- for(j=0;j<threadcount;j++)
- {
- pthread_join(threadid[j], NULL);
- }
- free( Rangearr);
- free(threadid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement