Multi Threading
Multi Threading
Date:
Program:
#include <pthread.h>
#include <stdio.h>
int sum[2];
void *thread1(void *arg) {
int d=(int)arg;
int i=0;
int start=d*250;
while(i<=250)
{sum[d]+=(i+start);
i++; } }
void *thread2(void *arg) {
int d=(int)arg;
int i=0;
int start=d*250;
while(i<=250)
{sum[d]+=(i+start);
i++; } }
int main()
{
pthread_t tid1,tid2; /* the thread identifier */
/* create the thread */
pthread_create(&tid1,NULL,thread1,(void *)0);
pthread_create(&tid2,NULL,thread2,(void *)1);
/* wait for the thread to exit */
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
printf("\n%d\t%d",sum[0],sum[1]);
return 0;
}
Output: