0% found this document useful (0 votes)
7 views

Multi Threading

The document describes a C program that uses two threads to calculate the sum of numbers from 0 to 500 by splitting the work between the threads, with one thread calculating numbers from 0 to 250 and the other from 251 to 500. The program creates two threads using pthread_create and waits for them to finish using pthread_join before printing the sums.

Uploaded by

Sujay Nithish
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Multi Threading

The document describes a C program that uses two threads to calculate the sum of numbers from 0 to 500 by splitting the work between the threads, with one thread calculating numbers from 0 to 250 and the other from 251 to 500. The program creates two threads using pthread_create and waits for them to finish using pthread_join before printing the sums.

Uploaded by

Sujay Nithish
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Exp no: Multithreading

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:

You might also like